2016-03-09 151 views
1

我无法在tkinter的Listbox Widget中插入多张图片。当我插入新图像时,前一个图像消失了。我该怎么办?下面是我在使用麻烦的代码:插入多张图片Tkinter

img = PhotoImage(file = Client.dir + "emo1.gif") 
self.listBox.insert(END, sender) 
self.listBox.image_create(END, image=img) 
self.listBox.insert(END, "\n") 
self.listBox.yview(END) 
+2

如果你没有保存对每个的引用,他们可能被垃圾收集。 –

+0

我该怎么办?如何引用他们每个人? – user6036956

+0

即时通讯创建聊天消息在哪里你可以发送图像 – user6036956

回答

2

作为the documentation状态:无论一个image选项是由一些小部件支持(和comment by @BryanOakley

然后图像对象可以被使用(例如标签,按钮,菜单)。在这些情况下,Tk将不会保留对图像的引用。删除图像对象的最后一个Python引用时,图像数据也会被删除,并且无论使用图像的哪个位置,Tk都会显示一个空框。

虽然suggestion by @BryanOakley肯定是最简单的解决方案,它可以防止未使用的图像从所收集的被垃圾这可能是不希望的。

注:我建议的解决方案是假设self.listBoxText小部件,因为Listbox小部件不具有image_create方法。如果您使用的是不同类型的小部件,那么您仍然可以创建一个类似的类来处理引用所使用的图像。

你可以做的Text一个子类,它保持到通过覆盖相关的方法插入图像的参考(image_createdelete是最重要的):

from tkinter import Text #, PhotoImage, Tk 

class Text_autoReferenceImage(Text): 
    def __init__(self,*varg,**kw): 
     self.images = {} 
     Text.__init__(self,*varg,**kw) 

    def image_create(self,index,**options): 
     img = options.get("image",None) 
     name = Text.image_create(self,index,**options) 
     if img is not None: 
      self.images[name] = img #this may remove previous reference with same name but different image 
     return name 

    def delete(self,*varg,**kw): 
     Text.delete(self,*varg,**kw) 
     self.clean_up_images() 

    def clean_up_images(self): 
     """deletes reference to all images that are no longer present in Text widget (called by .delete())""" 
     images_still_in_use = self.image_names() 
     for name in set(self.images.keys()): #need to put .keys() into a set in python3 or it complains about dictionary changing size during iteration 
      if name not in images_still_in_use: 
       del self.images[name] 

    def destroy(self): 
     self.images.clear() #remove all references to own images 
     return Text.destroy(self) 

那么如果self.listBox是这个类的一个实例而不是Text它将为您处理图像参考。

+1

感谢麦当劳詹森。现在我知道事情如何在Python中工作。非常丰富的答案。谢谢 :) – user6036956