從資料庫中檢索影像并渲染所有影像時,我遇到了“tkinter”和“canvas”問題。除錯代碼時所有影像正常顯示,但執行代碼影像未顯示在畫布中。
count = 0
posx = 1
posy = 5
for imagem in self.imagens:
img1 = Image.open(imagem[2])
img1 = img1.resize((200, 200), Image.ANTIALIAS)
photoImage = ImageTk.PhotoImage(img1)
self.canvas.create_image(posy, posx, anchor=NW, image=photoImage)
count = count 1
if count == 9:
break
if count == 0 or count == 3 or count == 6:
posy = 5
elif count == 1 or count == 4 or count == 7:
posy = 210
elif count == 2 or count == 5 or count == 8:
posy = 415
if count > 2 and count < 5:
posx = 205
elif count > 5 and count < 8:
posx = 410
uj5u.com熱心網友回復:
這些影像在退出類方法后被垃圾收集(正如您self在代碼中使用的那樣)。使用串列(實體變數)來存盤這些影像:
count = 0
posx = 1
posy = 5
self.imagelist = [] # for storing those images
for imagem in self.imagens:
img1 = Image.open(imagem[2])
img1 = img1.resize((200, 200), Image.ANTIALIAS)
photoImage = ImageTk.PhotoImage(img1)
self.imagelist.append(photoImage) # save the reference of the image
self.canvas.create_image(posy, posx, anchor=NW, image=photoImage)
count = count 1
if count == 9:
break
if count == 0 or count == 3 or count == 6:
posy = 5
elif count == 1 or count == 4 or count == 7:
posy = 210
elif count == 2 or count == 5 or count == 8:
posy = 415
if count > 2 and count < 5:
posx = 205
elif count > 5 and count < 8:
posx = 410
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/365156.html
