我有一个带有图像的标签和一个按钮,该按钮应该更新标签/删除标签中的图像,这样我就可以通过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
现在我只想,如果我们点击按钮,品牌预览会丢失图像/图像会被删除
编辑: 主要的问题已经解决了,但只有当按钮只需要删除图像时,这才起作用。如果我想删除和添加一个新的,它不起作用
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)
经过一番搜索,我找到了这个解决方案
这肯定会清除按钮上的图像。我没试过换个新形象。
我只是从网上抄来的,而且很管用。我用
我是用python 2.7完成的,我唯一的导入是
import Tkinter as tk
您非常接近
image
参数只需要一个空字符串,而不是None
。相关问题 更多 >
编程相关推荐