0

I would like to create a self-report questionnaire using tkinter. This questionnaire has a number of questions, and for every question the user should respond using numerical values ranging from 0 to 4 (where "0" stands for "absolutely not" while "4" stands for "absolutely yes")

I use Labels to pack the questions and Radiobuttons for the user's response.

What I want to do is to obtain for every question, firstly the index of the specific question and then the relative response chosen by the user. Here's the part of the code when I create the response Radiobuttons:

class Questionnaire:

    ...

    # response alternatives (from 0 to 4)
    def add_resps(self):
        self.question_index = {}
        self.var_list = []
        for i in range(len(self.affs)): # "self.affs" is the list of questions
            self.question_index[i] = i
            var = IntVar()
            self.var_list.append(var)
            for r in range(len( self.resps )):
                col_Resp = 5 # previous columns are occupied by questions
                self.wNumResp = Radiobutton(self.affs_frame,
                                       text=r,
                                       variable= self.var_list[i],
                                       value=r,
                                       command= lambda: self.get_resp(
                                                        self.question_index[i],
                                                        self.var_list[i]
                                                        ),
                                       bg="white",
                                       fg="black",
                                       font='Arial 10 bold',
                                       relief=SOLID)
                self.wNumResp.grid(row=i, column=r+colRisp, sticky=N+E+S+W)

    def get_resp(self, question, response ):
        print 'question n.', question, 'user\'s response:', response.get()

however... when I test if the code works by clicking on the radiobuttons I always get the same output whatever radiobutton I select at whatever question I respond:

>>> 
question n. 28 user's response: 0
question n. 28 user's response: 0
question n. 28 user's response: 0
question n. 28 user's response: 0
question n. 28 user's response: 0
question n. 28 user's response: 0
question n. 28 user's response: 0
question n. 28 user's response: 0
question n. 28 user's response: 0

Could anyone help me, please?

thanks in advance

armoschiano
  • 119
  • 2
  • 9

1 Answers1

1

This is a common problem people face when specifying the command for a control inside a loop. All of your radio buttons will use the same value of i in their commands, even though they all had different values when you created them. See Local variables in Python nested functions for an in-depth explanation of variable binding behavior. The practical solution is to provde i as a default argument:

command= lambda i=i: self.get_resp(
                self.question_index[i],
                self.var_list[i]
                ),
Community
  • 1
  • 1
Kevin
  • 74,910
  • 12
  • 133
  • 166