1

I have a problem with Python tkinter images. Have a brief look at a code bellow, and then read the rest. My problem is that image from self.file_with_puzzle() is not showing up. However when I put just the (testing) code from that class in between self.file_with_puzzle() and self.g.mainloop() it is working (As shown in the code). My guess is that it has sth to do with mainloop() but as I am a beginner programmer and there is no any error in Python Shell, I have no idea what the problem is. There are also some restrictions, mainloop() has to be in __init__(), but could be put earlier or later, but still within the __init__(). (And is there so when you launch the file outside the idle it won't immediately close). Also no calling anything else besides MyClass() at the end of code.

import tkinter, math, random

class MyClass:

    def __init__(self):
        self.g = tkinter.Canvas(width=800,height=800)
        self.g.pack()

        #self.game_board() Not needed right now, that's why it is commented
        #self.game_objects() Same as above
        self.file_with_puzzle()
        #thing bellow works but not wanted, just to show that it works this way
        """
        picture = tkinter.PhotoImage(file='photo.jpg')
        self.g.create_image(100,100, image=picture)
        """ 
        self.g.mainloop() # mainloop has to be in __init__()

        .
        .
        .

    """ FILE SECTION (5) """

    def file_with_puzzle(self):
        .
        .
        .
        # code here is just simple version for testing, otherwise it's more complex
        picture = tkinter.PhotoImage(file='photo.jpg')
        self.g.create_image(100,100, image=picture)

    """ END (5) """

MyClass()
#can call only MyClass() nothing else here
Matis
  • 611
  • 2
  • 11
  • 27
  • @Kevin Yes, the same problem. I didn't know it works this way. I couldn't find such question /answer. Well, thank you. Should we delete this question then ? – Matis Jan 08 '15 at 16:18

0 Answers0