【发布时间】:2016-01-23 00:23:01
【问题描述】:
我有一个带有图像的标签,以及一个应该更新标签/删除标签中图像的按钮,因此我可以通过 label.config 将新图像放入同一标签中。
我尝试使用类似的东西:每当您单击按钮时,它应该使用 label.config(image = None) 删除图像,但它不起作用,如果我将新图像加载到标签中,旧图像仍然存在那里:
# here is the label initialized
global brand_preview
brand_preview = Label(root, image = None)
brand_preview.place(x = 10, y = 60)
# thats the button which have to clear the label image
self.top_brand = Button(root, text = "clear", bg = "snow3", command=clear_label_image)
self.top_brand.place(x = 550, y = 60)
# thats how I load a photoimage into the label
photoimg_brand = ImageTk.PhotoImage(im_thumb)
brand_preview.image = photoimg_brand
brand_preview.config(image = photoimg_brand)
# Thats how the button works
def clear_label_image():
brand_preview.config(image = None)
brand_preview.image = None
我现在想要的是,如果我们单击按钮,brand_preview 会丢失图像/图像会被删除
编辑: 主要问题已解决,但仅当按钮只需删除图像时才有效。如果我想删除并添加一个新的,它不起作用
def clear_label_image():
brand_preview.config(image = "")
photoimg_brand = ImageTk.PhotoImage(im_thumb)
brand_preview.image = photoimg_brand
brand_preview.config(image = photoimg_brand)
【问题讨论】:
-
请发布一个带有可运行代码的最小示例,显示您要修复的行为。
-
您是否正在保存对新图像的引用?在您的新代码中,
photoimg_brand将被垃圾收集并在clear_label_image()完成后立即消失。
标签: python image tkinter label