Simple GUI with images

I am trying to display some maps on my simple graphical interface, but this is not displayed.

I downloaded a working file with the .gif that I want to download, and the code is closed, but not right. http://www.filedropper.com/cardgameproblem Size: 0Kb, Type: .zip

  • Contains 1.py and 1.gif

The lines you need to see start at 257-266. I know this is a small mistake, but I do not see it.

Please, help

0
source share
1 answer

After some deliberation, I found that the image displays correctly when saved in a GUI variable, 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 it works, but to name a variable is just gif1 (or any other name) without self , no. Tested both in code and in a minimal example.

Update: As @Bryan pointed out, the garbage collector provides an instance of PhotoImage when __init__ ends. You should keep a reference to an instance that is outside the scope of the constructor, for example. using self or global . Given this problem, it would be better to create a dict by saving images using map names as keys.

+1
source

Source: https://habr.com/ru/post/1492455/


All Articles