Skip to content Skip to sidebar Skip to footer

How To Create An Image Displayer Using Python Tkinter More Efficiently?

import tkinter as tk from PIL import ImageTk, Image root = tk.Tk() def photogetter(): ###global photo photo= ImageTk.PhotoImage(Image.open('smiley.png').resize((320,240))

Solution 1:

This is because when photo is not global it is garbage collected by the python garbage collector hence you need to keep reference to the image, this can be done so by saying global image or label.image = photo. Either way you just have to keep a reference so that it is not garbage collected.

global might not be efficient with OOP as it could create some issues later, is what i heard, so you can keep a reference by label.image = photo.

From effbot.org:

The problem is that the Tkinter/Tk interface doesn’t handle references to Image objects properly; the Tk widget will hold a reference to the internal object, but Tkinter does not. When Python’s garbage collector discards the Tkinter object, Tkinter tells Tk to release the image. But since the image is in use by a widget, Tk doesn’t destroy it. Not completely. It just blanks the image, making it completely transparent.

Hope this solved your doubts.

Cheers

Post a Comment for "How To Create An Image Displayer Using Python Tkinter More Efficiently?"