作为the documentation状态:无论一个image
选项是由一些小部件支持(和comment by @BryanOakley)
然后图像对象可以被使用(例如标签,按钮,菜单)。在这些情况下,Tk将不会保留对图像的引用。删除图像对象的最后一个Python引用时,图像数据也会被删除,并且无论使用图像的哪个位置,Tk都会显示一个空框。
虽然suggestion by @BryanOakley肯定是最简单的解决方案,它可以防止未使用的图像从所收集的被垃圾这可能是不希望的。
注:我建议的解决方案是假设self.listBox
是Text
小部件,因为Listbox
小部件不具有image_create
方法。如果您使用的是不同类型的小部件,那么您仍然可以创建一个类似的类来处理引用所使用的图像。
你可以做的Text
一个子类,它保持到通过覆盖相关的方法插入图像的参考(image_create
和delete
是最重要的):
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
它将为您处理图像参考。
如果你没有保存对每个的引用,他们可能被垃圾收集。 –
我该怎么办?如何引用他们每个人? – user6036956
即时通讯创建聊天消息在哪里你可以发送图像 – user6036956