How to set values for Radiobuttons values to default which were created through a loop in tkinter?

Viewed 51

I have looked into a similar post as mine Link but setting value to 0 does not work. I have also tried self.Btna.select()

I copied this code from here but amended it to my needs.

     self.vars = []
     for idx,i in enumerate(self.info):
         self.var = IntVar(value=0)
         self.vars.append(self.var)
         self.lblOption = Label(main,text=i)
         self.btnYes = Radiobutton(main, text="Yes", variable=self.var, value=2)
         self.btnNo = Radiobutton(main, text="No", variable=self.var, value=1)
         self.btnNa = Radiobutton(main, text="N/A", variable=self.var,value=0)
         self.lblOption.grid(column=1,row=idx, sticky = E)
         self.btnYes.grid(column=2,row=idx)
         self.btnNo.grid(column=3,row=idx)
         self.btnNa.grid(column=4,row=idx)

this creates me a nice smooth radiobuttons along with some labels. But I am trying to reset all values back to N/A if some where selected as yes or no.

I have tried self.Btna.select() but that deselects the last item on the list but not all.

Also, tried the following:

        for a in self.btnNa:
                a.select()

with an error of:

TypeError: can only concatenate str (not "int") to str
1 Answers

You need to set the value of variable of radiobutton to change its value.

Try,

for intvar in self.vars:
    intvar.set(0)

This should reset all back to value 0 which is "N/A".

Related