Skip to content Skip to sidebar Skip to footer

Python Tkinker - Showing A Jpg As A Class Method Not Working

I'm trying to show a jpg image as background for a GUI thing I'm building. I can get it to work in a single method: from Tkinter import * from PIL import Image, ImageTk class Mak

Solution 1:

The most obvious problem is that in the second case you are never calling showImage. Even after you do call that function, your image probably won't show up. Images will be garbage-collected if there isn't a reference to them. It may seem like there's a reference because you're adding it to a canvas, but that isn't enough.

You'll need to do something like:

self.photo = ImageTk.PhotoImage(image)

Finally, I recommend that you take the call to mainloop out of showImage. mainloop must always be called exactly once, so most typically it is the last line of code in your program, or the last line of code in your main function.

A more common way to make a Tkinter application is to subclass either the Tk object or a Frame object, rather than having your main application be a generic object. For example:

class MyApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        ...
        self.setupCanvas(...)
        ...
if __name__ == "__main__":
    app = MyApp()
    app.mainloop()

Post a Comment for "Python Tkinker - Showing A Jpg As A Class Method Not Working"