如何从所有条目中获取数据?

发布于 2025-01-23 21:06:11 字数 844 浏览 2 评论 0原文

我写了一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浴红衣 2025-01-30 21:06:11

您的保存函数循环循环在输入小部件列表上,但除了最后一个值以外的所有值。如果要打印每个值,请将打印语句移动到循环中。如果要创建具有所有值的数组,请将每个值附加到列表。

def save():
    my_array = []
    for entry in entries:
        my_array.append(entry.get())
    print(my_array)

但是,该循环可以凝结成清单理解:

def save():
    my_array = [entry.get() for entry in entries]
    print(my_array)

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.

def save():
    my_array = []
    for entry in entries:
        my_array.append(entry.get())
    print(my_array)

Though, that loop can be condensed into a list comprehension:

def save():
    my_array = [entry.get() for entry in entries]
    print(my_array)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文