使用画布显示图像

时间:2020-05-17 12:51:35

标签: python mysql tkinter

我无法在循环内使用画布显示图像。我已将图像目录存储在 mysql数据库。仅在最终循环运行时显示图像

图片:

enter image description here

from tkinter import *
from tkinter import messagebox
from tkinter import ttk
import mysql.connector
z=mysql.connector.connect(host='localhost',user='root',passwd='*****',database='***')
a=z.cursor()

spl=Tk()
a.execute('select * from spl') #fetching the image directory from databse
splval=IntVar()
b=a.fetchall()
for j in b:
        canvas=Canvas(width=100,height=100,bg='black')
        canvas.pack()  #the image is displayed only at last loop :(
        p=PhotoImage(file=j[2])
        canvas.create_image(0,0,image=p,anchor=NW)    
        rb1=Radiobutton(spl,text=j[1],variable=splval,value=j[0],bg='#F4F570',font='Consolas 20 bold')
        rb1.pack()

Label(spl,text=' ',bg='#F4F570').pack()
b1=Button(spl,text='SUBMIT',command=vsplv,font='Consolas 20 bold')
b1.pack()

spl.mainloop() 

1 个答案:

答案 0 :(得分:0)

感谢答复者:)

    picvar=[]
    for j in b:
        picvar.append(j[1])
        lvar=len(picvar)-1
        canvas=Canvas(width=100,height=100,bg='black')
        canvas.pack()
        picvar[lvar]=PhotoImage(file=j[2])
        canvas.create_image(0,0,image=picvar[lvar],anchor=NW)  

这将起作用