1

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()
Alex Held
  • 123
  • 11
  • 1
    Your indentation looks wrong. And why are you running mainloop in a loop? That makes absolutely no sense whatsoever. mainloop is already an infinite loop without putting it in another infinite loop. – Bryan Oakley Mar 01 '15 at 20:21
  • sorry formatting error in stack overflow. I fixed maintop. Still doesn't work – Alex Held Mar 01 '15 at 21:41
  • Your formatting still looks a bit odd. Is `self.maskPng = Im.open("foo.png")` supposed to be outside of `__init__`? That's a highly unusual way to construct a GUI. – Bryan Oakley Mar 01 '15 at 22:23

1 Answers1

0

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.

Marcin
  • 215,873
  • 14
  • 235
  • 294