In my tkinter application, I have a toplevel window with radio buttons. When I select any one of the radio button it is not returning any value nor printing any errors.
A minimal example is as follows:
import tkinter
from tkinter import *
from tkinter import ttk
class eventReg:
def __init__(self, master):
self.master = master
self.drawBoard()
def drawBoard(self):
self.addButton = ttk.Button(self.master, text="Add New Registration", command=self.newRegistration)
self.addButton.grid(row=1,column=1, sticky='w')
def newRegistration(self):
self.new_registration = tkinter.Toplevel(self.master)
self.new_registration.geometry('700x450+300+200')
self.radioVar = StringVar(master=self.new_registration)
self.nr_radio1 = ttk.Radiobutton(self.new_registration, text="Provide Unique Code", value="provide unicode", variable = self.radioVar)
self.nr_radio2 = ttk.Radiobutton(self.new_registration, text="Scan QR Code", value="scancode", variable=self.radioVar)
self.nr_radio1.grid(row=2, column=1, padx=15, pady=15, sticky='n')
self.nr_radio2.grid(row=2, column=2, padx=15, pady=15, sticky='n')
if self.radioVar.get() == "provide unicode":
self.test_Label = ttk.Label(self.new_registration, text="Working")
self.test_Label.grid(row=2, column=4, padx=15, pady=15)
if __name__ == '__main__':
master=Tk()
eventReg(master)
master.title('Event Registration Manager')
master.mainloop()
All you need is to bind a callback to your radio buttons in your eventReg
class.
class eventReg:
...
def newRegistration(self):
...
self.radioVar = StringVar(master=self.new_registration)
self.nr_radio1 = ttk.Radiobutton(self.new_registration, text="Provide Unique Code", value="provide unicode", variable = self.radioVar,command=self.get_var_value)
self.nr_radio2 = ttk.Radiobutton(self.new_registration, text="Scan QR Code", value="scancode", variable=self.radioVar,command=self.get_var_value)
...
def get_var_value(self):
if self.radioVar.get() == "provide unicode":
self.test_Label = ttk.Label(self.new_registration, text="Working")
self.test_Label.grid(row=2, column=4, padx=15, pady=15)