0

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

Codeja
  • 15
  • 4
  • 2
    Please just post the code which is giving you problems, modified if necessary for us to understand it. I think few people (including me) are willing to download a zip and go through hundreds of lines of code. – BrtH Oct 25 '12 at 11:29

1 Answers1

2

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.

tobias_k
  • 81,265
  • 12
  • 120
  • 179
  • 1
    The reason is that the python garbage collector will throw away the image when it detects there are no references to it. By saving it as an attribute of a class instance you prevent the garbage collector from throwing it away. Using a local variable like "gif1" causes it to be garbage collected when the function exits. – Bryan Oakley Oct 25 '12 at 13:51
  • THANKYOU SO MUCH tobias_k. I 'accepted' your answer, but unfortunately I can't give rep yet (I'm new to the site.. <15). I can't tell you how much I appreciate it though – Codeja Oct 26 '12 at 02:09