我正在嘗試建立一個程式來收集用戶的資料。為此,用戶設定輸入框的數量并創建輸入框。我的問題是關于當用戶設定 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()
uj5u.com熱心網友回復:
使用串列來存盤這些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 widget
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/350126.html
下一篇:第26行,在__init__tk.Tk().__init__(self,*args,**kwargs)中。型別錯誤:create()引數1必須是str或None,而不是App
