Search code examples
pythonimagetkinterlabel

Python Tkinter remove/delete Image from Label


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.

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

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

EDIT: 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)

Solution

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

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