0

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.

user2063
  • 975
  • 4
  • 15
  • 35
  • It seems to me that your formatting string is wrong, as the number of parameters does not match the number of specifiers. Have a look in the Python doc (http://docs.python.org/library/stdtypes.html#string-formatting-operations) and try to fiddle with your print statement. – Michael Schlottke-Lakemper Jul 12 '12 at 07:22
  • Thanks for the link but I can't seem to get it to work as I need to wait until all my boxes are filled.. =S – user2063 Jul 12 '12 at 08:37
  • I have changed my approach since I know the other way works with one value at least. – user2063 Jul 12 '12 at 09:14

2 Answers2

1

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
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • I'm in the process of re organising my code. I haven't long started so it's been a bit difficult but I appreciate your help. – user2063 Jul 12 '12 at 13:13
  • Although your answer doesn't address *this* question, you address the overall design. So it is actually valuable beyond this question. +1. – gary Jul 12 '12 at 13:59
0

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))   
Community
  • 1
  • 1
gary
  • 4,227
  • 3
  • 31
  • 58
  • The first part of your answer seems like bad advice to me. The code in the question (at the time I write this) will only add the value to `self.values` once. That means that once someone edits the text in the entry widget, that new text will never show up in `self.values`. – Bryan Oakley Jul 12 '12 at 14:47
  • @BryanOakley I guess all I could say is it needs to be declared outside of `numberwritten()` so it's not empty each time that method is called. – gary Jul 13 '12 at 01:28