Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 217
Roman Avatar asked Oct 23 '15 12:10

Roman


2 Answers

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

def clear_label_image():
    brand_preview.config(image='')
like image 111
TigerhawkT3 Avatar answered Sep 30 '22 17:09

TigerhawkT3


After some Googling, I found this solution

def clear_label_image():
    #brand_preview.config(image = None)
    brand_preview.image.blank()
    brand_preview.image = None

This definitely clears the image from the button. I haven't tried changing to a new image.

I just copied it from the Web, and it worked. I created the image with

photoimg_brand = tk.PhotoImage(file='/usr/share/httpd/icons/world1.gif')

I did this with python 2.7, and my only import was import Tkinter as tk

like image 40
saulspatz Avatar answered Sep 30 '22 19:09

saulspatz