Question

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).

I would like to do for example 'Value %s" % i = value obtained.

Was it helpful?

Solution

self.values = [] is clearing the list each time you call numberwritten() so that is why you are printing just one value. Declare self.values globally or in the __init__() function in your class.

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))   

OTHER TIPS

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. 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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top