python tkinter canvas 不显示图像
python tkinter with canvas not showing images
当从数据库中检索图像并渲染所有图像时,我遇到了“tkinter”和“canvas”问题。调试代码时所有图像正常显示,但执行代码图像不显示 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
这些图像在退出 class 方法后被垃圾回收(因为您在代码中使用了 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
当从数据库中检索图像并渲染所有图像时,我遇到了“tkinter”和“canvas”问题。调试代码时所有图像正常显示,但执行代码图像不显示 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
这些图像在退出 class 方法后被垃圾回收(因为您在代码中使用了 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