0

I am running a script with tkinter that captures user input and then opens a second and possibly a third window based on the input. The issue I am having is capturing user input from the third and final window. Each window is divided up into it's own python class on execution.

Here is the code that calls the third window, which executes properly:

test_assign = TestAssign(mylist)

Here is the third window code:

class TestAssign:
    def __init__(self, mylist):
        self.mylist = mylist
        self.selected_values = []
        self.master = Tk()
        for i in range(len(mylist)):
             setattr(self, 'item'+mylist[i], IntVar())
             ch = Checkbutton(master, text='item'+mylist[i], variable=getattr(self, 'item'+mylist[i])
             ch.pack()
        b = Button(master, text='Next', command=self.get_selected_values)
        b.pack()

        mainloop()

    def get_selected_values(self):
        for i in range(len(self.mylist)):
            if getattr(self, 'item'+self.mylist[i]) == 1:
                self.selected_values.append(self.mylist[i])
        self.master.destroy()

Control then returns to the call point (at least I believe it does). Where I attempt to print the selected values:

test_assign = TestAssign(mylist)
while not test_assign.selected_values:
    pass
print test_assign.selected_values

Everytime execution gets to the print statement it prints an empty list whether there are boxes checked or not. If I call dir(test_assign) for testing purposes, the checkbox attrs are there. Not sure why I am not able to capture it like this.

Can anyone see the flaw in my code?

1 Answer 1

1

Two things: 1)

ch = Checkbutton(master, text='item'+mylist[i], variable=getattr(self, 'item'+mylist[i])

and

b = Button(master, text='Next', command=self.get_selected_values)

I think master should be self.master (but honestly, that almost certainly just a copy/pasting error.)

2) The important one:

if getattr(self, 'item'+self.mylist[i]) == 1:

should be

if getattr(self, 'item'+self.mylist[i]).get() == 1:

(you need to call get on your IntVars to read the value.)

Sign up to request clarification or add additional context in comments.

1 Comment

lol...thank you!! I'll try it right now and accept your answer in a min

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.