Python Tkinter remove/delete 来自标签的图像

Python Tkinter remove/delete Image from Label

我有一个带有图像的标签,以及一个应该更新标签/删除标签中图像的按钮,因此我可以通过 label.config.[=14 将新图像放入同一标签中=]

我尝试使用类似的东西:每当你点击按钮时,它应该使用 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

这肯定会清除按钮上的图像。我还没试过更换新图片。

我刚刚从网上复制了它,它起作用了。我用

创建了图像
photoimg_brand = tk.PhotoImage(file='/usr/share/httpd/icons/world1.gif')

我用 python 2.7 做了这个,我唯一的导入是 import Tkinter as tk

如果您使用 label 显示图像,那么您可以这样做:

label.pack_forget()

label 应该是全局的