Rom*_*man 4 python label image tkinter
我有一个带图像的标签,以及一个按钮,它应该更新标签/删除标签中的图像,这样我就可以通过label.config将新图像放入同一个标签中.
我尝试使用类似的东西:每当你点击按钮时,它应该用label.config(图像=无)删除图像,但它不起作用,如果我将新图像加载到标签中旧的图像仍然存在:
# 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
Run Code Online (Sandbox Code Playgroud)
我现在想要的是,如果我单击按钮,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)
Run Code Online (Sandbox Code Playgroud)
你非常接近 - image参数只需要一个空字符串而不是None.
def clear_label_image():
brand_preview.config(image='')
Run Code Online (Sandbox Code Playgroud)