I am building a Python desktop application using tkinter library. The application must create a list of entries based on a spinbox value when generate_button clicked.
I am wondering how can I delete the list of entries and recreate new entries if the user changed the spinbox value and clicked the generate_button a second time.
Following is my code ...
CORRECT_ENTRIES = []
def generate_entries():
n = int(spinbox.get())
for i in range(1, n 1):
entry = Entry(width=40)
entry.grid(row=3 i, column=0, pady=5, padx=5)
CORRECT_ENTRIES.append(entry)
window = Tk()
# Spinbox
spinbox = Spinbox(from_=3, to=15, width=5)
spinbox.grid(row=2, column=1)
# Button
generate_btn = Button(text="Generate Entries", command=generate_entries)
generate_btn.grid(row=2, column=3)
CodePudding user response:
In case someone is looking for a solution for a such issue I found the following solution and It worked properly hopefully:
def remove_old_entries():
for entry in COOKIES:
entry.grid_remove()
for entry in EXTRA_COOKIES:
entry.grid_remove()
def generate_entries():
global x, COOKIES, EXTRA_COOKIES
remove_old_entries()
COOKIES = []
EXTRA_COOKIES = []
n = int(spinbox.get())
for i in range(1, n 1):
r_entry = Entry(width=40)
r_entry.grid(row=3 i, column=1, pady=5, padx=5)
COOKIES.append(r_entry)
m = int(spinbox_extra.get())
for i in range(1, m 1):
entry = Entry(width=40)
entry.grid(row=3 i, column=2, pady=5)
EXTRA_COOKIES.append(entry)
I called this function inside the generate_entries() function