tkinter中的数组状态



我写了一段代码,我想在输入元素时对其进行改进,它首先只允许我将数据插入第一个索引,然后它允许第二个框将数据插入等等。如何禁用Array其他元素的状态?

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):
element = box_list[i].get() # Get value from corresponding Entry
ArrayLabel=tk.Label(ArrayR,text="Array Element: " + element,font="Arial 12 bold",bg="red",fg="white",bd="5")
ArrayLabel.pack()
box_list = []   # Create list of Entrys
def Boxes():
xx=size.get()
for i in range(xx):        
box=tk.Entry(ArrayR,font="Arial 10 bold",bd="5",width="5")
box.pack(side="left")
box_list.append(box)    # Append current Entry to list
ApplytoLabel1=tk.Button(ArrayR,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")
ArrayR = tk.Frame(root)
ArrayR.pack()

root.mainloop()

这是一种方法:

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):
if box_list[i].cget('state') == 'normal':
element = box_list[i].get() # Get value from corresponding Entry
ArrayLabel=tk.Label(ArrayR,text="Array Element: " + element,font="Arial 12 bold",bg="red",fg="white",bd="5")
ArrayLabel.pack()
box_list[i].configure(state='disabled')
try:
box_list[i+1].configure(state='normal')
except IndexError: pass
break
box_list = []   # Create list of Entrys
def Boxes():
xx=size.get()
for i in range(xx):
box=tk.Entry(ArrayR,font="Arial 10 bold",bd="5",width="5",state='disabled' if i else 'normal')
box.pack(side="left")
box_list.append(box)    # Append current Entry to list
ApplytoLabel1=tk.Button(ArrayR,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")
ArrayR = tk.Frame(root)
ArrayR.pack()
root.mainloop()

不过,通常情况下,您可能希望每个按钮都在lambda回调中,ApplytoLabel接收一个参数,这样您就不必每次都循环(和中断(。我也会把所有的东西都包在一个班里。

相关内容

  • 没有找到相关文章

最新更新