Tkinter is a bit of an unusual beast, because there are two languages involved here - Python (in which you write your code), and Tcl (which hosts the Tk user interface toolkit). There are some oddities in the interaction between the two environments, and you've run into perhaps the most glaring of them. Specifically, there is no proper synchronization between the lifetime of an actual PhotoImage object (which lives entirely on the Tcl side), and the Python PhotoImage object which acts as a proxy to it. Currently, if the Python object is ever garbage-collected, the Tcl object is automatically deleted, even though there might still be a Tcl-side reference to the image. The consequence of this is that you CANNOT use only a local variable (like image
in your code) for the Python reference to the image, as it will go away at the end of the function. Storing the image in a global variable would work, but might give the image too long of a lifetime (it would never be deleted, even if the window using it was closed). Storing it as an instance variable would work (self.image
instead of image
, in your case), as the Python instance will presumably be around exactly as long as the Tcl widget it describes. Another popular option is to store the image as an attribute of the widget that uses the image.