使用图像更新 Tkinter 标签

Updating Tkinter Label with an Image

好吧,我在使用 Tkinter 和 PIL 更新标签时遇到了 "small" 问题。

只要我按下 cheese 按钮,它就应该显示 foo2,但它只显示一个白屏。有什么建议吗?

这是我的代码:

from PIL import Image as Im
from Tkinter import *

class MyWindow():

    def __init__(self):
        self.root = Tk()

        self.maskPng = Im.open("foo.png")
        self.maskPng.convert("RGBA")
        self.maskPng.save("bar.gif", "GIF")

        self.mask = PhotoImage(file = "bar.gif")

        self.show = Label(self.root, image = self.mask).pack(side = "left")

        self.speedBTN = Button(self.root, text = "cheese", command = self.speed).pack(side = "right")

        self.changed = False


   def speed(self):
        self.speedImg = Im.open('foo2')
        self.speedImg = self.speedImg.convert("RGBA")

        # overlaying foo and foo2 -- it works I tested it
        self.maskPng.paste(self.speedImg, (0,0), self.speedImg)


        self.render()

    def render(self):
        self.mask = PhotoImage(self.speedImg)
        self.show.configure(image = self.mask)
        self.show.image = self.mask

    def draw(self):
        self.root.mainloop()


    main = MyWindow()
    main.root.mainloop()

嗯,我认为原因是 self.show,当您按下按钮时,它是 None。这是因为这一行:

self.show = Label(self.root, image = self.mask).pack(side = "left")

应该是:

self.show = Label(self.root, image = self.mask)
self.show.pack(side = "left")

发生这种情况,因为 packgrid 等 return None.