Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
988 views
in Technique[技术] by (71.8m points)

python - Updating Tkinter Label with an Image

Well I got a "small" problem with updating a label using Tkinter and PIL.

As soon as I press the cheese Button it should display foo2, but it just displays a white screen. Any suggestions?

Here is my code:

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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well, I think that the reason is self.show which is None when you press the button. This is because this line:

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

should be:

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

This happens, because pack, grid etc. return None.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...