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