Python Tkinter 从标签中删除/删除图像

标签 python image tkinter label

我有一个带有图像的标签,以及一个应该更新标签/删除标签中图像的按钮,因此我可以通过 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='')

关于Python Tkinter 从标签中删除/删除图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33302779/

相关文章:

python - 计算 pandas 中的 Tf-Idf 分数?

python - python中的PLS-DA算法

html - Web 开发 - 如何保证某些元素在背景图像上的位置?

python - Numpy ndarray 图像像素平均值大于零的像素值 : Normalizing image

python - 怎么写这个更 "Pythonically"

python - Pandas 计算行中逐年(或任何其他索引)的变化

python - 使用 OpenCV 进行裂纹检测之前去除背景纹理图案

Python 和 Tkinter

python - 使用 tkinter 将文件路径从浏览按钮放入全局变量中

python - 更新 celery 任务已完成的客户端