如何从 Tk window 中删除一组条目?
How to remove a set of entries from a Tk window?
我正在使用 tkinter 库构建一个 Python 桌面应用程序。单击 generate_button 时,应用程序必须基于旋转框值创建条目列表。
我想知道如果用户更改旋转框值并再次单击 generate_button,我该如何删除条目列表并重新创建新条目。
以下是我的代码...
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)
万一有人正在寻找此类问题的解决方案,我找到了以下解决方案并且希望它能正常工作:
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)
我在 generate_entries() 函数中调用了这个函数
我正在使用 tkinter 库构建一个 Python 桌面应用程序。单击 generate_button 时,应用程序必须基于旋转框值创建条目列表。
我想知道如果用户更改旋转框值并再次单击 generate_button,我该如何删除条目列表并重新创建新条目。
以下是我的代码...
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)
万一有人正在寻找此类问题的解决方案,我找到了以下解决方案并且希望它能正常工作:
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)
我在 generate_entries() 函数中调用了这个函数