Feb-06-2024, 03:22 PM
(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.
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.
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>> >>>
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-=1This 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 += 1as 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