Gexas
Gexas

Reputation: 678

Tkinter radio button not passing value

I am tryng to dynamicaly create buttons after radioButton selection, but I can't find out how to pass value from radioButton to my function. What I am doing wrong?

My relevant part of the code:

class VP_info():
    def __init__(self):

        #radio buttons
        self.radio_var = tk.IntVar()
        radioBtt1 = ttk.Radiobutton(self.window, text="Registracija", variable = self.radio_var, value= 0, command=self.radioBtt_click)
        radioBtt2 = ttk.Radiobutton(self.window, text="Keitimai", variable = self.radio_var, value= 1, command=self.radioBtt_click)
        radioBtt1.grid(row=2, column=1, sticky="w")
        radioBtt2.grid(row=2, column=1)
     #radioBtt click event       
    def radioBtt_click(self):
        first_frame = ttk.LabelFrame(self.window, text="VP info", relief=tk.RIDGE)
        first_frame.grid(row=4, column=1,  padx=10, pady=10, sticky=tk.E + tk.W + tk.N + tk.S)
        if self.radio_var == 0:
            self.add_buttons_first(first_frame)
        else:
            self.add_buttons_second(first_frame)

With with code, then I click on radio button, always second set of buttons is being created (despite the fact that first radio button is selected).

Upvotes: 1

Views: 140

Answers (1)

amanb
amanb

Reputation: 5463

You should get the value of self.radio_var in the function:

def radioBtt_click(self):
    #<---code-->
    if self.radio_var.get() == 0:
        self.add_buttons_first(first_frame)
    else:
        self.add_buttons_second(first_frame)

Upvotes: 2

Related Questions