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.

3
  • 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 (docs.python.org/library/…) and try to fiddle with your print statement. Commented Jul 12, 2012 at 7: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 Commented Jul 12, 2012 at 8:37
  • I have changed my approach since I know the other way works with one value at least. Commented Jul 12, 2012 at 9:14

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

2 Comments

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.
Although your answer doesn't address this question, you address the overall design. So it is actually valuable beyond this question. +1.
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))   

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.