murzagurskiy
murzagurskiy

Reputation: 1323

Images disappearing in Tkinter

I tried to outputting images in label. There are two images, which need to appear in labels several times. And they have appeared only once - in the latter labels.

class Application(tk.Frame):
    def __init__(self, master=None):
        #some actions

    def ShowImages(self, frame_in, type_img, place_img):
        print type_img
        print place_img
        print frame_in
        self.image = Image.open(type_img + ".png")
        self.image = self.image.resize((20, 20), Image.ANTIALIAS) #The (250, 250) is (height, width)
        self.photo = ImageTk.PhotoImage(self.image)
        label = tk.Label(frame_in, image=self.photo, relief='sunken', borderwidth=2)
        label.pack(side="right")

        self.image2 = Image.open(place_img + ".png")
        self.image2 = self.image2.resize((20, 20), Image.ANTIALIAS) #The (250, 250) is (height, width)
        self.photo2 = ImageTk.PhotoImage(self.image2)
        label = tk.Label(frame_in, image=self.photo2, relief='sunken', borderwidth=2)
        label.pack(side="right")

def createWidgets(self, dict_of_data):
        frame = tk.Frame(self, relief='sunken')
        frame.grid(row=0, column=self.index, sticky="WN")
        frame_in = tk.Frame(frame)
        frame_in.grid(row=0, sticky="WE", column=self.index)

        header = tk.Label(frame_in, anchor="nw", justify="left", text="Игра: ")
        header.pack(expand=True, fill="x", side="left")

        self.ShowImages(frame_in, dict_of_data["type"], dict_of_data["place_type"])
        #some other code

if __name__ == "__main__":
    app = Application()
    app.master.title('Sample application')
    #All that data is not real data of my script
    app.createWidgets({'name':"", 'state':"", "type":"", "date":{"start":"", 'end':""}, 'duration':{'days':'', 'hours':''}, 'site':'', 'rank':''})
    app.createWidgets({'name':"", 'state':"", "type":"", "date":{"start":"", 'end':""}, 'duration':{'days':'', 'hours':''}, 'site':'', 'rank':''})
    app.createWidgets({'name':"", 'state':"", "type":"", "date":{"start":"", 'end':""}, 'duration':{'days':'', 'hours':''}, 'site':'', 'rank':''})
    app.mainloop()

enter image description here

So, in two words: I tried to invoke function ShowImages three times, and i want to see 6 images (3 x 2 images), but i see only the last 2. Names of images are identical.

I think this is trouble with opening images. Maybe there is some rule, how i can use one image several times.

P. S. Sorry for my english. I didn't know, how i need to describe my trouble. Thanks.

Upvotes: 0

Views: 3421

Answers (1)

murzagurskiy
murzagurskiy

Reputation: 1323

I have solved my problem! Thanks @FabienAndre and THIS post. I just realized, that every time i called function, old value of self.photo and self.photo2 variables are cleared and images disappeared.

For solve this trouble, i prepare all images i needed in Class constructor, and every time just use same value in variable.

class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.initImages()                                #Prepare images
        self.master.resizable(width=False, height=False)
        self.index = 0
        self.grid()

    def initImages(self):
        self.images = {}
        buf = Image.open("Classic.png")
        buf = buf.resize((20, 20), Image.ANTIALIAS) #The (250, 250) is (height, width)
        self.images['Classic'] = ImageTk.PhotoImage(buf)

        buf = Image.open("Jeopardy.png")
        buf = buf.resize((20, 20), Image.ANTIALIAS) #The (250, 250) is (height, width)
        self.images['Jeopardy'] = ImageTk.PhotoImage(buf)

        buf = Image.open("On-site.png")
        buf = buf.resize((20, 20), Image.ANTIALIAS) #The (250, 250) is (height, width)
        self.images['On-site'] = ImageTk.PhotoImage(buf)

        buf = Image.open("On-line.png")
        buf = buf.resize((20, 20), Image.ANTIALIAS) #The (250, 250) is (height, width)
        self.images['On-line'] = ImageTk.PhotoImage(buf)

    def ShowImages(self, frame_in, type_img, place_img):
        label = tk.Label(frame_in, image=self.images[type_img])
        label.pack(side="right")

        label = tk.Label(frame_in, image=self.images[place_img])
        label.pack(side="right")

    def createWidgets(self, dict_of_data):
        frame = tk.Frame(self, relief='sunken')
        frame.grid(row=0, column=self.index, sticky="WN")
        frame_in = tk.Frame(frame)
        frame_in.grid(row=0, sticky="WE", column=self.index)
        #some other code here

result of solution

P.S. I know, my english is ridiculous, but i have no practice... Sorry

Upvotes: 2

Related Questions