I'm trying to display some cards on my simple GUI, but it's not showing up.

I've uploaded a working file with the .gif I want to upload, and the code is close but not right. http://www.filedropper.com/cardgameproblem Size: 0Kb, Type:.zip

  • Contains 1 .py and 1 .gif

The lines to look at start at 257-266. I know it's just a minor error, but I can't see it.

Please help


Solution 1:

After some tinkering, I found out that the image is properly displayed when stored in a variable of the GUI, i.e. using self

self.gif1 = PhotoImage(file='1.gif')
self.canvas.create_image(0, 0, image=self.gif1, anchor=NW)

I have no idea why, but This works, while naming the variable just gif1 (or any other name), without self, does not. Tested both in your code and in a minimum example.

Update: As @Bryan pointed out, the garbage collector disposes the PhotoImage instance when __init__ finishes. You have to keep a reference to the instance beyond the scope of the constructor, e.g. using self, or global. Given the problem at hand, it might be best to create a dict, storing the images using the names of the cards as keys.