Python Forum
make widgets disappear from tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
make widgets disappear from tkinter
#11
(Feb-05-2024, 08:37 PM)deanhystad Wrote: I didn't know there was a forget. It is never mentioned anywhere in the documentation. So I asked Python about it.
Output:
>>> import tkinter as tk >>> root = tk.Tk() >>> label = tk.Label(root, text="Label") >>> label.forget <bound method Pack.pack_forget of <tkinter.Label object .!label>> >>>
forget() is just another name for pack_forget(). forget() only works if you use pack() to position the widgets. forget() does nothing if you use grid() to place widgets.

I assume forget() is the main problem, but this is also a problem.
    self.day_lbl[-1].forget()
    self.act_lbl[-1].grid_forget()
    self.day_opt[-1].grid_forget()
    self.act_opt[-1].grid_forget()
    self.unit_lbl[-1].grid_forget()
    self.unit_ent[-1].grid_forget()
    self.min_lbl[-1].grid_forget()
    self.count-=1
This code might work the first time you call it, but probably not the second. Can you guess why? After you try to erase a row, what is in self.act_lbl? does len(self.act_lbl) == self.count? Is it important that they are the same? I can only guess because you didn't post the relevant code.

Please post short runnable examples. Someone reading your post would have no idea what day_lbl[-1] is or what day_lbl[-1].forget() should do. Posting examples that can be run (having all the imports and setup code) serves as documentation and lets others see what you see when you run your code.

If you don't spend time on your posts making it easy for others to help you, others will be less inclined to help you.

ok got it. sorry for not making a decent post. first i have this code that gets triggered when the add entry button is pressed:


    def add_column(self):
        '''generates a column of entries and labels
                    names of each entry and label are created dynamically
                    and each widget has a location added. Tkinter "rows" are incremented so each time this function is called
                    the next column of widgets gets generated below the previous'''
        self.cur_row += 1


        self.day_lbl.append(Label(text="On"))
        self.day_lbl[-1].grid(row=self.cur_row, column=0)
        self.day_opt.append(OptionMenu(self.window,self.day_value_inside, self.updated_list[0], self.updated_list[1],
                                       self.updated_list[2], self.updated_list[3], self.updated_list[4],
                                       self.updated_list[5], self.updated_list[6], self.updated_list[7],
                                       self.updated_list[8], self.updated_list[9]))
        self.day_opt[-1].grid(row=self.cur_row, column=1)
        self.act_lbl.append(Label(text="i did"))
        self.act_lbl[-1].grid(row=self.cur_row,column=2)
        self.act_opt.append(OptionMenu(self.window,self.act_value_inside,"Aerobics", "Cycling","Running", "Swimming", "Walking"))
        self.act_opt[-1].grid(row=self.cur_row,column=3)
        self.unit_lbl.append(Label(text="for: "))
        self.unit_lbl[-1].grid(row=self.cur_row,column=4)
        self.unit_ent.append(Entry())
        self.unit_ent[-1].grid(row=self.cur_row,column=5)
        self.min_lbl.append(Label(text="minutes"))
        self.min_lbl[-1].grid(row=self.cur_row,column=6)
        self.count += 1
as you can see the count attribute is used to represent the length of the lists. then i have this remove entry code which follows


    def remove_button(self):
        if self.count>=1:

            self.day_lbl[-1].grid_forget()
            self.act_lbl[-1].grid_forget()
            self.day_opt[-1].grid_forget()
            self.act_opt[-1].grid_forget()
            self.unit_lbl[-1].grid_forget()
            self.unit_ent[-1].grid_forget()
            self.min_lbl[-1].grid_forget()
            self.count-=1
        else:
            messagebox.showerror(title="Ooooops",message="You have no more entries to delete")
the problem with this code is that it removes the entry the first time i press the button. even though i add more columns before removing entry again it fails to remove the entry as it should
Reply
#12
I don't see where you remove the entry from the list. Changing count doesn't do anything to the list. grid_forget() only erases the widget from the window. It doesn't destroy the widget or remove the widget from the list. Your remove/append code doesn't work due to sloppy housekeeping. This is the equivalent of sweeping the dirt under the rug. You can't see it, but it is still there.
Reply
#13
I don't see where you remove the entry from the list. Changing count doesn't do anything to the list. grid_forget() only erases the widget from the window. It doesn't destroy the widget or remove the widget from the list. Your remove/append code doesn't work due to sloppy housekeeping. This is the equivalent of sweeping the dirt under the rug. You can't see it, but it is still there.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why I am getting ModuleNotFoundError when I make an Exe file for tkinter GUI? pymn 0 1,663 Apr-01-2022, 05:36 PM
Last Post: pymn
  [Tkinter] It it possible to make a help file explorer with tkinter? Clunk_Head 0 1,998 Aug-07-2021, 06:02 PM
Last Post: Clunk_Head
  .get() from generated Entry widgets in tkinter snakes 4 4,257 May-03-2021, 11:26 PM
Last Post: snakes
  [Tkinter] tkinter.Menu – How to make text-variable? Sir 3 5,673 Mar-10-2021, 04:21 PM
Last Post: Sir
  Tkinter Python: How to make sure tkSimpleDialog takes in No value entered rcmanu95 3 2,363 Aug-05-2020, 05:32 AM
Last Post: Yoriz
  How to make button text bold in Tkinter? scratchmyhead 2 12,101 May-16-2020, 02:53 AM
Last Post: scratchmyhead
  Using Tkinter widgets on child window chewy1418 8 7,271 Feb-27-2020, 10:34 PM
Last Post: Marbelous
  Make Label Text background (default color) transparent using tkinter in python barry76 1 23,863 Nov-28-2019, 10:19 AM
Last Post: Larz60+
  Tkinter - Make changes to graph and update it adriancovaci 0 6,616 Apr-08-2019, 09:02 AM
Last Post: adriancovaci
  [Tkinter] Multiple frames with tkinter - How to make them run on fullscreen mode eabs86 3 18,284 Sep-20-2018, 01:27 AM
Last Post: eabs86

Forum Jump:

User Panel Messages

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