简体   繁体   English

从Entry小部件获取值并创建变量Tkinter列表

[英]Get values from Entry widgets and create list of variables Tkinter

I have the following code which works and gives me each value. 我有以下代码可以正常工作,并给我每个值。 I can't seem to create a list of all the values once they are collected. 收集所有值后,我似乎无法创建所有值的列表。 I'm currently getting one value at a time..! 我目前一次获得一个价值。 I've tried the two following ways: 我尝试了以下两种方法:

def numberwritten(self, event, key, index):
    self.values = []
    if not self.left[index]:
        self.box[key].config(fg='black')
        self.left[index] = True
        self.values.append(self.box[key].get())
        print self.values

This works with just one value being printed each time but I would like a list I can assign 'n' variables to (the number of values obtained changes at random each time). 每次只能打印一个值,但是我想要一个列表,我可以为其分配“ n”个变量(每次获取的值的数量随机变化)。

I would like to do for example 'Value %s" % i = value obtained. 我想做例如'Value%s“%i =获得的值。

You are creating rows of widgets. 您正在创建小部件行。 Each row may have more or less widgets than other rows. 每行可能比其他行具有更多或更少的小部件。 At some point you need to get a list that represents the data in each row. 在某个时候,您需要获得一个代表每一行中数据的列表。 You're asking, "how do I get this list?". 您在问,“我怎么得到这个清单?”。 Am I correct? 我对么?

You must have asked 20 questions about this one simple problem. 您必须已经针对这个简单问题问了20个问题。 The problem isn't in this or any other single function, it's in your general architecture. 问题不在于此功能或任何其他单一功能,而在于您的一般体系结构。 A very, very smart programmer once told me "if you want me to understand your code, don't show me your code, show me your data structures". 一个非常非常聪明的程序员曾经告诉我“如果您希望我理解您的代码,请不要显示您的代码,请显示您的数据结构”。 The root problem here is, you have no data structures. 根本问题在于,您没有数据结构。 If you don't organize your data, you can't hope to get at the data easily. 如果您不整理数据,就无法希望轻松获得数据。

There's nothing hard about this problem. 这个问题没有什么难的。 Keep a list of the entry widgets for each row, and iterate over that list whenever you need the values. 保留每行条目窗口小部件的列表,并在需要这些值时遍历该列表。 The following pseudocode shows how simple this can be: 以下伪代码显示了它的简单程度:

class MyApp(object):
    def __init__(self):
        # this represents your data model. Each item in the 
        # dict will represent one row, with the key being the
        # row number 
        self.model = {}

    def add_row(self, parent, row_number):
        '''Create a new row'''
        e1 = Entry(parent, ...)
        e2 = Entry(parent, ...)
        e3 = Entry(parent, ...)
        ...
        # save the widgets to our model
        self.model[row_number] = [e1, e2, e3]

    def extend_row(self, parent, row_number, n):
        '''Add additional widgets to a row'''
        for i in range(n):
            e = Entry(parent, ...)
            self.model[row_number].append(e)

    def get_values(row_number):
        '''Return the values in the widgets for a row, in order'''
        result = [widget.get() for widget in self.model[row_number]]
        return result

self.values = [] is clearing the list each time you call numberwritten() so that is why you are printing just one value. self.values = []会在您每次调用numberwritten()时清除列表,这就是为什么只打印一个值的原因。 Declare self.values globally or in the __init__() function in your class. 全局或在类的__init__()函数中声明self.values

In terms of formatting for printing one value during each loop iteration, try something like this: 关于在每次循环迭代期间打印一个值的格式,请尝试如下操作:

print("Value %i = %s" % (index, self.box[key].get())

Edit: I borrowed the following from this answer if you actually want to print your entire list each loop iteration: 编辑:我借鉴了以下这个答案,如果你真的想打印整个列表中的每个循环迭代:

# Map items in self.values to strings (necessary if they are not strings).
print("All values: [%s]" % ", ".join(map(str, self.values)))

# Use this if the contents of self.values are strings.
print("All values: [%s]" % ", ".join(self.values))   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM