Reputation: 169
So I am now making a GUI application with tkinter and whatever number I put in the uppermost box, the GUI will automatically add another entry row
When I put 1, and then I try to bigger number such as 3 or 5 it works just fine ,the entry row will show up, the problem is whenever I put number such as 5 and the try to put number less than 5, the entry will still show up instead of removed
Is there any way that when I put bigger number such as 4 and followed by the less such as 2 and the, the entry from 5 will be removed??
Edit : add code
def choiceone(self):
self.labeln1 = ttk.Label(self, text="1", justify="center")
self.labeln1.grid(row=7, column=1)
self.entry1 = ttk.Entry(self, width=15)
self.entry1.grid(row=7, column=2)
self.entry2 = ttk.Entry(self, width=15)
self.entry2.grid(row=7, column=3)
self.entry3 = ttk.Entry(self, width=15)
self.entry3.grid(row=7, column=4)
self.entry4 = ttk.Entry(self, width=15)
self.entry4.grid(row=7, column=5)
def choicetwo(self):
self.labeln2 = ttk.Label(self, text="2", justify="center")
self.labeln2.grid(row=9, column=1)
self.entry5 = ttk.Entry(self, width=15)
self.entry5.grid(row=9, column=2)
self.entry6 = ttk.Entry(self, width=15)
self.entry6.grid(row=9, column=3)
self.entry7 = ttk.Entry(self, width=15)
self.entry7.grid(row=9, column=4)
self.entry8 = ttk.Entry(self, width=15)
self.entry8.grid(row=9, column=5)
def choicethree(self):
self.labeln3 = ttk.Label(self, text="3", justify="center")
self.labeln3.grid(row=11, column=1)
self.entry9 = ttk.Entry(self, width=15)
self.entry9.grid(row=11, column=2)
self.entry10 = ttk.Entry(self, width=15)
self.entry10.grid(row=11, column=3)
self.entry11 = ttk.Entry(self, width=15)
self.entry11.grid(row=11, column=4)
self.entry12 = ttk.Entry(self, width=15)
self.entry12.grid(row=11, column=5)
class MultiLevel(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = ttk.Label(self, text="Fatigue Failure", font=LARGE_FONT)
label.grid(columnspan=3, sticky="w")
labele1 = ttk.Label(self, text="METHOD 1", font=LARGE_FONT)
labele1.grid(row=1, column=1)
ttk.Separator(self, orient=tk.HORIZONTAL).grid(row=2, columnspan=5, sticky="EW")
labele0 = ttk.Label(self, text="")
labele0.grid(row=3, column=1)
label0 = ttk.Label(self, text="Number of \nStress Level: ", font=MEDIUM_FONT, justify="center")
label0.grid(row=4, column=1)
self.entry0 = ttk.Entry(self, width=15)
self.entry0.grid(row=4, column=2)
button0 = ttk.Button(self, text="OK ", command=lambda: self.ok())
button0.grid(row=4, column=3)
def ok(self):
try:
float(self.entry0.get())
except ValueError:
errormsg("INPUT YOUR NUMBER")
else:
if float(self.entry0.get()) == 1:
choiceone(self)
elif float(self.entry0.get()) == 2:
choiceone(self)
choicetwo(self)
elif float(self.entry0.get()) == 3:
choiceone(self)
choicetwo(self)
choicethree(self)
Upvotes: 0
Views: 268
Reputation: 46669
As @furas said, you need to destroy the existing widgets first:
def ok(self):
try:
float(self.entry0.get())
except ValueError:
errormsg("INPUT YOUR NUMBER")
else:
# remove existing widgets start from row 7
for widget in self.grid_slaves():
if int(widget.grid_info()["row"]) >= 7:
widget.destroy()
if float(self.entry0.get()) == 1:
choiceone(self)
...
PS: As from your code, the concern widgets are put in row 7 and above.
The suggestion is based on your code, but I think you better refactor your code.
Upvotes: 0
Reputation: 142641
Everytime you put numer you alway create new Entries in grid but you never remove previous Entries so don't expect that they will magically disappear.
You have to use grid_forget()
to hide widget (but not remove from memory), or grid_remove()
to remove from grid and from memory.
Now when you put 1 then you create entries in first row, and when you put 3 you create again entries in first row so you have two entries in every cell in first row - one above another - previous entries don't disappear.
BTW: It could be easier if you would keep entries on list - self.entries[0]
, self.entries[1]
, etc. or as 2-dimensional list self.entries[row][col]
.
Upvotes: 1