Skip to content
Advertisement

Why does Tkinter image not show up if created in a function?

This code works:

JavaScript

It shows me the image.

Now, this code compiles but it doesn’t show me the image, and I don’t know why, because it’s the same code, in a class:

JavaScript

Advertisement

Answer

The variable photo is a local variable which gets garbage collected after the class is instantiated. Save a reference to the photo, for example:

JavaScript

If you do a Google search on “tkinter image doesn’t display”, the first result is this:

Why do my Tkinter images not appear? (The FAQ answer is currently not outdated)

Advertisement