Python Forum
tkinter destroy label inside labelFrame
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter destroy label inside labelFrame
#1
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.
Reply
#2
what is linking these files?
I don't see any imports.
Reply
#3
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.
Reply
#4
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter: An image and label are not appearing. emont 7 410 Mar-21-2024, 03:00 PM
Last Post: deanhystad
  Using Tkinter inside function not working Ensaimadeta 5 4,864 Dec-03-2023, 01:50 PM
Last Post: deanhystad
  [Tkinter] can i had a cefpython3 to a labelframe razs 25 10,938 Sep-15-2023, 01:38 PM
Last Post: munirashraf9821
  [Tkinter] Help running a loop inside a tkinter frame Konstantin23 3 1,451 Aug-10-2023, 11:41 AM
Last Post: Konstantin23
Exclamation [Tkinter] Error when closing the main window with destroy TomasSanchexx 1 728 Aug-06-2023, 01:54 AM
Last Post: deanhystad
  [Tkinter] Updating Tkinter label using multiprocessing Agusms 6 3,054 Aug-15-2022, 07:10 PM
Last Post: menator01
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,672 Jun-26-2022, 06:26 PM
Last Post: menator01
  [Tkinter] _tkinter.TclError: can't invoke "destroy" command: application has been destroyed knoxvilles_joker 6 15,278 Apr-25-2021, 08:41 PM
Last Post: knoxvilles_joker
  [Tkinter] Redirecting all print statements from all functions inside a class to Tkinter Anan 1 2,603 Apr-24-2021, 08:57 AM
Last Post: ndc85430
  Menu destroy Heyjoe 5 3,284 Mar-02-2021, 01:45 AM
Last Post: Heyjoe

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020