如何从所有条目中获取数据?
我写了一个DEF来创建带有条目的N*n矩阵。我想从所有条目中获取输入的数据,但我总是只得到最后一个。
from tkinter import *
import numpy as np
root = Tk()
root.geometry('800x300')
root.title('PythonExamples.org - Tkinter Example')
global e1
global numm
global my_entry
entries=[]
my_entry= Entry(root)
e1=Entry(root)
e1.place(x=200,y=100)
def create():
numm=int(e1.get())
global my_entry
for x in range(numm):
for i in range(numm):
my_entry = Entry(root)
my_entry.grid(row=x, column=i)
entries.append(my_entry)
def save():
for entry in entries:
my_array=entry.get()
print(my_array)
create= Button(root,text='Submit',command=create).place(x=40,y=180)
save= Button(root,text='Save',command=save).place(x=40,y=210)
my_label=Label(root,text='')
root.mainloop()
我该如何解决?提前致谢。
I wrote a def to create an n*n matrix with entries. I want to get the inputed data from all of the entries but I always got only the last one.
from tkinter import *
import numpy as np
root = Tk()
root.geometry('800x300')
root.title('PythonExamples.org - Tkinter Example')
global e1
global numm
global my_entry
entries=[]
my_entry= Entry(root)
e1=Entry(root)
e1.place(x=200,y=100)
def create():
numm=int(e1.get())
global my_entry
for x in range(numm):
for i in range(numm):
my_entry = Entry(root)
my_entry.grid(row=x, column=i)
entries.append(my_entry)
def save():
for entry in entries:
my_array=entry.get()
print(my_array)
create= Button(root,text='Submit',command=create).place(x=40,y=180)
save= Button(root,text='Save',command=save).place(x=40,y=210)
my_label=Label(root,text='')
root.mainloop()
How can I solve it? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
保存
函数循环循环在输入小部件列表上,但除了最后一个值以外的所有值。如果要打印每个值,请将打印语句移动到循环中。如果要创建具有所有值的数组,请将每个值附加到列表。但是,该循环可以凝结成清单理解:
Your
save
function loops over the list of entry widgets, but throws away every value but the last. If you want to print out each value, move the print statement inside the loop. If you want to create an array with all of the values, append each value to a list.Though, that loop can be condensed into a list comprehension: