【问题标题】:Array tkinter Entry to Label数组 tkinter 条目到标签
【发布时间】:2018-10-31 08:24:19
【问题描述】:

大家好,我是初学者,正在使用 Tkinter 开发 Project Linear 和 Binary search GUI 应用程序,我想在此处添加多个条目框值到标签和数组中,我尝试过,但它不能正常工作:

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()

【问题讨论】:

    标签: python python-3.x tkinter


    【解决方案1】:

    好吧;它工作不正常并不是什么问题描述,但我猜你想以某种方式保留数组元素。执行此操作的常用方法是创建一个列表,然后在创建条目时将条目添加到列表中。

    当您创建标签时,您只需从条目列表中读取值,因为它们与标签具有相同的索引。部分代码:

    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()
    

    【讨论】:

    • 它工作了,现在我想在进入第二个盒子之前让它变得高效,应该有必要在第一个盒子中添加价值......
    • 可以,但是代码会复杂很多。您基本上必须将焦点转移到任何动态创建的条目上,然后以您想要的任何方式处理它。当用户按下“提交到数组”按钮时,检查所有条目会容易得多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多