伙计们,我是新手,正在使用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()
答案 0 :(得分:0)
好吧;它不能很好地工作并不是问题的描述,但是我猜你想以某种方式保留数组元素。通常的方法是创建一个列表,然后在创建条目时将其添加到列表中。
创建标签时,您只需从条目列表中读取值即可,因为它们与标签具有相同的索引。部分代码:
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()