Python Tkinter从标签中删除/删除图像 [英] Python Tkinter remove/delete Image from Label

查看:1592
本文介绍了Python Tkinter从标签中删除/删除图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a label with an image, and a button which should update the label / delete the image in the label, so I can put a new image into the same label via label.config.

我尝试使用类似的方法:每当您单击按钮时,它都应该使用label.config(image = None)删除图像,但是它不起作用,如果我将新图像加载到标签中,旧图像仍然存在在那里:

I tryed to use something like that: whenever you click on the button the it should remove the image with label.config(image = None) but it doesnt work, if I load new images into the label the old ones are still there:

    # 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会丢失图像/图像被删除

All I want now that if we I click the Button the brand_preview loses the image / the image gets deleted

主要问题已解决,但仅在按钮仅需删除图像时才起作用.如果我要删除并添加一个新的文件,则不起作用

The main issue is solved, but that only works if the button only has to delete the image. If I want to delete and add a new one it doesnt work

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.

You're very close - the image parameter just needs an empty string rather than None.

def clear_label_image():
    brand_preview.config(image='')

这篇关于Python Tkinter从标签中删除/删除图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆