图像在Tkinter中消失

时间:2015-03-23 16:33:17

标签: image python-2.7 tkinter python-imaging-library

我试图在标签中输出图像。有两个图像需要多次出现在标签中。他们只出现过一次 - 在后者的标签中。

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

所以,用两个词:我试图调用函数ShowImages三次,我想看到6个图像(3 x 2图像),但我只看到最后2个。图像的名称是相同的。

我认为这是打开图像的麻烦。也许有一些规则,我怎么可以多次使用一个图像。

P上。 S.对不起,我的英文。我不知道,我怎么需要描述我的麻烦。感谢。

1 个答案:

答案 0 :(得分:0)

我已经解决了我的问题! 谢谢@FabienAndre和THIS发帖。 我刚刚意识到,每次调用函数时,self.photo和self.photo2变量的旧值都会被清除,图像也会消失。

为了解决这个问题,我准备了我在Class构造函数中需要的所有图像,并且每次只在变量中使用相同的值。

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。我知道,我的英语很荒谬,但我没有练习......对不起