CREATE TABLE `student_profile` (
`id` int NOT NULL,
`student` varchar(10) NOT NULL,
`profile_photo` longblob NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
COMMIT;
Using SQLite database
CREATE TABLE IF NOT EXISTS student_profile(id integer,
student text,profile_photo blob);
This can be linked with our student table and photos of the student can be displayed.
my_conn = create_engine("mysql+mysqldb://userid:password@localhost/database_name")
my_row=my_conn.execute("SELECT * FROM student_profile limit 0,4")
for student in my_row:
pass
Here we used student to collect data.
e = Label(my_w, text=student[0])
e.grid(row=i,column=1,ipadx=20)
e = Label(my_w, text=student[1])
e.grid(row=i,column=2,ipadx=60)
In above code we will add one line images.append(img) to keep refrence to all images in a directory images[]. Without this line only the last image will be displayed. Here the image being used in the Button, or in a Label does not count as a reference for the garbage collector. import io
from PIL import Image, ImageTk
stream = io.BytesIO(student[2])
img=Image.open(stream)
img = ImageTk.PhotoImage(img)
e = Label(my_w, image=img)
e.grid(row=i, column=3,ipady=7)
images.append(img) # garbage collection
import tkinter as tk
from tkinter import *
from sqlalchemy import create_engine
import io
from PIL import Image, ImageTk
my_w = tk.Tk() # parent window
my_w.geometry("400x500") # size as width height
my_w.title("www.plus2net.com") # Adding a title
# database connection
my_conn = create_engine("mysql+mysqldb://userid:password@localhost/database_name")
my_row=my_conn.execute("SELECT * FROM student_profile limit 0,4")
# Column headers row 0
l1=Label(my_w, text='ID')
l1.grid(row=0,column=1)
l2=Label(my_w, text='Name')
l2.grid(row=0,column=2)
l3=Label(my_w, text='Photo')
l3.grid(row=0,column=3)
i=1 # data starts from row 1
images = [] # to manage garbage collection.
for student in my_row:
stream = io.BytesIO(student[2])
img=Image.open(stream)
img = ImageTk.PhotoImage(img)
e = Label(my_w, text=student[0])
e.grid(row=i,column=1,ipadx=20)
e = Label(my_w, text=student[1])
e.grid(row=i,column=2,ipadx=60)
e = Label(my_w, image=img)
e.grid(row=i, column=3,ipady=7)
images.append(img) # garbage collection
i=i+1
my_w.mainloop()
08-08-2023 | |
id=mycur.execute("INSERT INTO pic(CARNO,NAME,IMG)"/ "VALUES ( %d,%s, %s)",data) TypeError: unsupported operand type(s) for /: 'str' and 'str' |