[英]Array tkinter Entry to Label
大家好,我是新手,正在使用Tkinter開發Project Linear和Binary搜索GUI應用程序,我想添加多個Entry box值來標記,並在此處的數組中嘗試了但無法正常工作:
import tkinter as tk
root=tk.Tk()
root.title("Looping of entry box")
root.geometry("1200x600")
def ApplytoLabel():
xx=size.get()
for i in range(xx):
ArrayLabel=tk.Label(root,text="Array Element")
ArrayLabel.pack()
def Boxes():
xx=size.get()
for i in range(xx):
box=tk.Entry(root)
box.pack()
ApplytoLabel1=tk.Button(root,text="Submit To Array",command=ApplytoLabel)
ApplytoLabel1.pack()
Array = tk.Frame(root)
Array.pack()
text1=tk.Label(Array,text="Enter the Size of Array:",
font="Arial 10 bold",fg="blue")
text1.grid(row=0,column=0,sticky="w")
size=tk.IntVar()
ArraySize=tk.Entry(Array,textvariable=size)
ArraySize.grid(row=0,column=1,sticky="w")
SizeofArray=tk.Button(Array,text="Submit",command=Boxes)
SizeofArray.grid(row=0,column=2,sticky="w")
root.mainloop()
好; 它不能很好地工作並不是問題的描述,但是我猜你想以某種方式保留數組元素。 通常的方法是創建一個列表,然后在創建條目時將其添加到列表中。
創建標簽時,您只需從條目列表中讀取值即可,因為它們與標簽具有相同的索引。 部分代碼:
def ApplytoLabel():
xx=size.get()
for i in range(xx):
element = box_list[i].get() # Get value from corresponding Entry
ArrayLabel=tk.Label(root,text="Array Element: " + element)
ArrayLabel.pack()
box_list = [] # Create list of Entrys
def Boxes():
xx=size.get()
for i in range(xx):
box=tk.Entry(root)
box.pack()
box_list.append(box) # Append current Entry to list
ApplytoLabel1=tk.Button(root,text="Submit To Array",command=ApplytoLabel)
ApplytoLabel1.pack()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.