我有一个带有图像的标签,以及一个应该更新标签/删除标签中图像的按钮,因此我可以通过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)
您非常接近-image
参数只需要一个空字符串而不是None
。
def clear_label_image():
brand_preview.config(image='')
经过一番谷歌搜索后,我找到了这个解决方案
def clear_label_image():
#brand_preview.config(image = None)
brand_preview.image.blank()
brand_preview.image = None
这肯定会清除按钮上的图像。我没有尝试过更改为新图像。
我只是从Web复制了它,然后它起作用了。我用
创建了图像photoimg_brand = tk.PhotoImage(file='/usr/share/httpd/icons/world1.gif')
我使用python 2.7做到了,我唯一的导入是import Tkinter as tk
如果使用label
显示图像,则可以执行此操作:
label.pack_forget()
[label
应该是全局的