我正在尝试使用 grid_slaves 从函数创建列网格数据列表。我希望该函数获取列中的每个网格值,然后附加到列表中。没有创建列表,但打印前 3 个列表会产生:
[[<tkinter.Entry object .!labelframe.!entry>]] [[<tkinter.Entry object .!labelframe.!entry2>]] [[<tkinter.Entry object .!labelframe.!entry3>]]
[[<tkinter.Entry object .!labelframe.!entry>], [<tkinter.Entry object .!labelframe.!entry7>]] [[<tkinter.Entry object .!labelframe.!entry2>], [<tkinter.Entry object .!labelframe.!entry8>]] [[<tkinter.Entry object .!labelframe.!entry3>], [<tkinter.Entry object .!labelframe.!entry9>]]
from tkinter import*
root = Tk()
def make_list(num_rows): # Make array of value columns
#Create empty lists
value1_list=[]
value2_list=[]
value3_list=[]
value4_list=[]
value5_list=[]
value6_list=[]
#Obtain values in frame 1 grid
for j in range(2,num_rows+2):
# Obtain values in each column
value1_column = frame1.grid_slaves(row=j, column=1)
value2_column = frame1.grid_slaves(row=j, column=2)
value3_column = frame1.grid_slaves(row=j, column=3)
value4_column = frame1.grid_slaves(row=j, column=4)
value5_column = frame1.grid_slaves(row=j, column=5)
value6_column = frame1.grid_slaves(row=j, column=6)
# Fill lists with frame 1 column values
value1_list.append(value1_column)
value2_list.append(value2_column)
value3_list.append(value3_column)
value4_list.append(value4_column)
value5_list.append(value5_column)
value6_list.append(value6_column)
print(value1_list, value2_list, value3_list) # output above
#Create Make List GUI
width= root.winfo_screenwidth()
height= root.winfo_screenheight()
root.title("Value Frame")
root.geometry("%dx%d" % (width, height))# Set GUI to full screen
frame1 = Frame(root) #Create frame for input boxes
frame1=LabelFrame(root, text='Value Frame 1',padx=10, pady=10)
frame1.grid(row=0, column=0)
#Frame 1 labels
label1 = Label(frame1, text='value 1',pady=2)
label1.grid(row=1, column=1)
label2 = Label(frame1, text='value 2',pady=2)
label2.grid(row=1, column=2)
label3 = Label(frame1, text='value 3',pady=2)
label3.grid(row=1, column=3)
label4 = Label(frame1, text='value 4',pady=2)
label4.grid(row=1, column=4)
label5 = Label(frame1, text='value 5',pady=2)
label5.grid(row=1, column=5)
label6 = Label(frame1, text='value 6',pady=2)
label6.grid(row=1, column=6)
num_rows = 2
for i in range(num_rows):
# Label row No
label1 = Label(frame1, text=f'# {i+1}',pady=2)
label1.grid(row= 2+i, column=0)
#Enter Data Frame 1
value1 = Entry(frame1,width=10)
value2 = Entry(frame1,width=10)
value3 = Entry(frame1,width=10)
value4 = Entry(frame1,width=10)
value5 = Entry(frame1,width=10)
value6 = Entry(frame1,width=10)
#Grid Location Frame 1
value1.grid(row=i+2, column = 1)
value2.grid(row=i+2, column = 2)
value3.grid(row=i+2, column = 3)
value4.grid(row=i+2, column = 4)
value5.grid(row=i+2, column = 5)
value6.grid(row=i+2, column = 6)
# Prefill grid values
value1.insert(0, int("5"))
value2.insert(0, int("2"))
value3.insert(0, int("4"))
value4.insert(0, int("8"))
value5.insert(0, int("6"))
value6.insert(0, int("1"))
#Create submit button and call Make List function
myButton = Button(root, text='Make List', command = lambda:make_list(num_rows))
myButton.grid(row = 0, column = 15, sticky = N, padx = 40, pady=10)
root.mainloop()
如有任何建议,将不胜感激。
尝试了多种不同的 grid_slave 配置,但无法创建列表
您缺少的部分是您必须在每个条目上调用
.get()
才能获取值。
此外,您还可以更好地利用循环。无论您有多少行或列,以下函数都可以工作,并且不需要那么多行代码。
我让它打印值并返回值。返回值是没有意义的,因为您是从按钮命令调用它的,但如果您想引用
lambda
内的值,或在 lambda
之外调用此函数,它就在那里。
def make_list(num_rows):
results = []
for r in range(2, num_rows+2):
# add a new row to our results
row = []
results.append(row)
for c in range(1,7):
# slaves will be a list with one value.
slaves = frame1.grid_slaves(row=r, column=c)
entry = slaves[0]
# get the value, append it to the list of values
# for this row
value = entry.get()
row.append(value)
print(results)
return results