Python Forum

Full Version: tkinter destroy label inside labelFrame
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello.I have 3 functions inside a file:

import webbrowser
from tkinter import *
from datetime import datetime
def open_google_maps(root , label_frame , see_info):
     
    show_message = Label(label_frame , text = str(datetime.now().strftime("%H:%M:%S")) + "        " * 4 + "Google maps link has opened")
    if(see_info):
        maps_URL = "https://www.google.com/maps/"
        browser = webbrowser.get("chromium")
        browser.open(maps_URL)
        show_message.place(x = 0 , y = 60)
    else:
        show_message.destroy()
 
 
def clear_labels(root , label_frame):
    open_google_maps(root , label_frame , False)
In another file I have this function:
def control_window(root):
 
    operation_info = LabelFrame(root , width = 400 , height = 100)
    operation_info.place(x = 1105 , y = 100)
    Label(operation_info , text = "Time                                      " + "Operation").place(x = 0 , y = 0)
 
    clear_operation_button = Button(root , text = "Clear all" , width = 5 , height = 5 , command = lambda: clear_labels(root , operation_info))
    clear_operation_button.place(x = 1035 , y = 100)
 
    open_google_button = Button(root , text = "GOOGLE MAPS" , width = 15 , height = 2 , command = lambda: open_google_maps(root , operation_info , True) )
    open_google_button.place(x = 20 , y = 85)
and it is called by another function like this:
root = Tk()
root.geometry("1500x1500")
control_window(root)
root.mainloop()
My problem is that when I click google maps button ,it shows some text in the labelFrame.So when I click clear all button I want to destroy this label(not labelFrame) ,but it doesn't destroy.How can I fix it?
My program is bigger and different that this one I show you.I make it smaller and a little bit different to make it easier to understand.

Thanks.
what is linking these files?
I don't see any imports.
I don't think it is a good idea destroying labels. Set the text to '' or unpack the label so it is not visible. If you need it once it is likely you will need it again.

If you want to make the widget not appear you need to unpack it. Packing tells a widget where it will appear and draws it. forget_pack and forget_grid tells the parent of the widget that the widget is no longer visible and would you please erase any mess left behind. That stuff you see after destroy is not a label, it is the left over image of the label. Use the appropriate forget method then destroy the label if you really think you need to.
Totally agree with you. Destroying labels outright can make it cumbersome if you need to bring them back later. It's way better to just empty the text or unpack it. And you're right about that residual image issue; 'forget' is the way to go to clean that up. Thanks for bringing this up, it's a good programming practice people should be aware of!