0

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.

4
  • All the labels show whatever is in self.photo and self.photo2. When you open a new image into self.photo(2), the new image is displayed. Instead, append each image to a list of images and use images_list[-1] for the Label's image= keyword
    – user4171906
    Commented Mar 23, 2015 at 17:14
  • It's have a sense, but then i need to create list if images and pass it to the function. I want to know, is this some way to clone or duplicate image in memory? Commented Mar 23, 2015 at 18:07
  • 1
    it is a way to keep a reference to the image in python, and avoid them of being garbage collected. Otherwise, only tcl reference exists in tkinter and python think nobody use it and it could free corresponding memory. Commented Mar 23, 2015 at 22:06
  • possible duplicate of Python Tkinter : gif image in a canvas Commented Mar 23, 2015 at 22:07

1 Answer 1

2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.