我无法在循环内使用画布显示图像。我已将图像目录存储在 mysql数据库。仅在最终循环运行时显示图像
图片:
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()
答案 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)
这将起作用