Jio
Jio

Reputation: 608

Cannot populate radio button correctly - Tkinter

I am trying out few things with Tkinter as an exercise. In my example app i want user to select one option from drop down list. Based on this selection i want to populate the list of radio button. I have created a list of values to populate in dropdown list and a dictionary for radio button. please check the code.

See below some working and not working examples: enter image description here

As you can see from the picture. The first window works well. Second also. However the third window is not quite correct. The option '410' remains. I am making subsequent selections without closing the application.

I think perhaps i am not declaring the variables at the right place. It would be very helpful if some one can have a look at the code and rectify it.

Code:

    from Tkinter import *
    import ttk


    class App(Frame):

        def __init__(self,parent):
            Frame.__init__(self)
            self.parent = parent
            self.v = IntVar()
            #self.radio_value = []
            #self.i = 0
            self.GUI()


        def GUI(self):

            self.master.title('Example')
            self.pack(fill = BOTH, expand = 1)
            self.options = ['a1','a2','a3','a4','a5']
            self.box_value = StringVar()
            self.box = ttk.Combobox( self, textvariable=self.box_value)
            self.box.bind("<<ComboboxSelected>>", self.set_Radio)
            self.box['values'] = self.options
            self.box.current(0)
            self.box.grid(row = 0, column = 0  )

            self.choices = {'a1':['30', '70', '140', '410'], 'a2': ['a', 'b', 'c'], 'a3': ['x', 'y', 'z'], 'a4':['p', 'q', 'r'], 'a5': ['l', 'm', 'n']}

        def set_Radio(self,parent):

            i = 0
            radio_value = []
            if self.box_value.get() in self.choices.keys():
                radio_value = self.choices[self.box_value.get()]
            print radio_value

            for t in radio_value:
                i = i+1
                b = Radiobutton(self, text=t,  variable=self.v, value=t)
                b.grid(row = i, column = 0)

    def main():
        root = Tk()
        root.geometry('250x250')
        app1= App(root)
        root.mainloop()


    if __name__ == '__main__':
        main()

Upvotes: 0

Views: 348

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385970

The problem is that you don't delete the old radiobuttons before creating the new radiobuttons. One solution is to put them in an invisible frame. When you delete the frame, the radiobuttons will automatically be destroyed. Another solution is to keep a reference to them so that you can destroy them individually later.

Here's an example of keeping a reference:

def __init-_(self, parent):
    ...
    self.radios = []

def set_Radio(self,parent):
    for widget in self.radios:
        widget.destroy()
    self.radios = []
    ...
    for t in radio_value:
        ...
        b = Radiobutton(...)
        self.radios.append(b)
        ...

Upvotes: 2

Related Questions