数组 tkinter 条目到标签 [英] Array tkinter Entry to Label
问题描述
嘿,伙计们,我是初学者,正在使用 Tkinter 开发项目线性和二进制搜索 GUI 应用程序,我想将多个条目框值添加到标签和此处的数组中,我尝试过,但无法正常工作:
Hey Guys I am beginner and working on Project Linear and Binary search GUI application using Tkinter, I want to add multiple Entry boxes values to label and in an array here, I tried but its not working fine:
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()
推荐答案
好吧;它不能正常工作并不是一个问题描述,但我猜你想以某种方式保留数组元素.执行此操作的常用方法是创建一个列表,然后在创建条目时将它们添加到列表中.
Well; it's not working fine is not much of a problem description but I'm going to guess you want to keep the array elements in some way. The usual way to do this is to create a list and then add the Entrys to the list as you create them.
当您创建标签时,您只需从条目列表中读取值,因为它们与标签具有相同的索引.部分代码:
When you create the Labels you simply read the values from the list of Entrys as they have the same index as the Labels. Part of the code:
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()
这篇关于数组 tkinter 条目到标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!