我正在尝试建立一个从用户那里收集数据的程序。为此,用户设置输入框的数量,并创建输入框。我的问题是,当用户设置4个输入框时,创建了4个输入框,之后,用户可以设置2个输入框,但仍然显示最后4个输入框,我的意思是,我需要删除之前用户创建的所有输入框,并创建新的输入框。我尝试过winfo_children(),但它删除了所有输入框,甚至包括由用户设置输入框数量的输入框。你能帮帮我吗?先谢谢你,
from tkinter import *
# Creating main screen
root = Tk()
# Function to set amount of entry boxes
def boxes():
# Saving amount of boxes
amount = int(number_boxes.get())
for i in range(amount):
for y in range(4):
set_boxes = Entry(root, borderwidth=3)
set_boxes.grid(row=i+4, column=y)
# Creating label for number of boxes
label_number_boxes = Label(root, text='Enter the number of boxes')
# Showing in screen
label_number_boxes.grid(row=0, column=0)
# Creating number of boxes
number_boxes = Entry(root, width=20, bg='white', fg='black', borderwidth=3)
# Showing in screen
number_boxes.grid(row=1, column=0)
# Creating button to set the amount of boxes
button_number_boxes = Button(root, text='Set amount of boxes', command=boxes)
# Showing in screen
button_number_boxes.grid(row=2, column=0)
# Creating infinite loop to show in screen
root.mainloop()发布于 2021-11-04 16:52:47
使用列表存储这些Entry小部件,然后可以在创建新的Entry小部件之前使用此列表销毁这些小部件:
entries = []
# Function to set amount of entry boxes
def boxes():
# clear old entry boxes
for w in entries:
w.destroy()
entries.clear()
# Saving amount of boxes
amount = int(number_boxes.get())
for i in range(amount):
for y in range(4):
set_boxes = Entry(root, borderwidth=3)
set_boxes.grid(row=i+4, column=y)
entries.append(set_boxes) # save the Entry widgethttps://stackoverflow.com/questions/69842698
复制相似问题