Python Tkinter remove/delete Image from Label
Asked Answered
A

4

11

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)
Anaesthesia answered 23/10, 2015 at 12:51 Comment(2)
Please post a minimal example with runnable code that displays the behavior you want to fix.Tolkan
Are you saving a reference to the new image? In your new code, photoimg_brand will get garbage-collected and disappear as soon as clear_label_image() completes.Rafter
R
26

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

def clear_label_image():
    brand_preview.config(image='')
Rafter answered 23/10, 2015 at 13:11 Comment(5)
Thanks that works if the Button has to delete it, but somehow if I add new Image after it the old one is again there, thats weird!Anaesthesia
What do you mean, the old one is there?Rafter
if the Button first has to clean the label and then give it a new image, then the old one comes back / is still there. All in all im trying to create a preview window. We have 2 Buttons that do the same, clean label and display an image on top or bottom of the label. If I click this button it has to remove the top image and update it with a new (the bot image). But I guess I can figure it out alone, you helped me alot already.Anaesthesia
There should be only 0 or 1 images in the label. I'm not seeing anything like the behavior you're describing.Rafter
Okay, thats probably my problem, all the code is really long so i tried to post only relevant parts, but I have probably to work with Canvas. Atm I am drawing another label ontop of a label so I have main label + small label on top of the main label. I update the images in the small label with this button, so they appear bottom or top of the main label.Anaesthesia
T
1

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

Tolkan answered 23/10, 2015 at 13:33 Comment(2)
where does blank comes from? Its an unkown attributeAnaesthesia
@RomanDammer I updated my answer, but I'm sorry to say I don't have very good information for you. I see you used ImageTk to create the PhotoImage. That's probably the difference.Tolkan
V
0

If you are using label to show the image then you can do this:

label.pack_forget()

label should be global

Vorticella answered 17/6, 2020 at 15:12 Comment(1)
HI katt, welcome on Stack Overflow. Please use proper english.Cementite
L
0

I have a solution using customtkinter.

While initialising the ui, have:

brand_preview = customtkinter.CTkLabel(master=self.frame_center_2, text = '', width = 200, height = 200)

And when you add the image in a later function (if not make another one to update the cover), have:

    urllib.request.urlretrieve(im_thumb, "cover.png") # Downloading image from url
    cover_image = customtkinter.CTkImage(Image.open("cover.png"), size = (250, 250))
    self.cover_label.configure(image = '')
    self.cover_label.configure(image = cover_image)
    self.cover_label.pack(pady = 2, padx = 2, expand = True, fill = 'both')
Lauzon answered 16/5 at 17:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.