Home > database >  I am trying to get the value from radiobutton in tkinter but getting eror as "NameError: name &
I am trying to get the value from radiobutton in tkinter but getting eror as "NameError: name &

Time:05-18

I am running into a problem with a variable. Not able to understand the error. My objective is to get value from Radiobutton with the below code.

import tkinter as tk


class App:
    def __init__(self, root):
        root.title("undefined")
        width=600
        height=500
        screenwidth = root.winfo_screenwidth()
        screenheight = root.winfo_screenheight()
        alignstr = '%dx%d %d %d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        root.geometry(alignstr)
        root.resizable(width=False, height=False)
        
        R1 = tk.Radiobutton(root, text="Option 1", variable=var, value='10',
                  command=sel)
        R1.place(x=100,y=70,width=85,height=25)
        R2 = Radiobutton(root, text="Option 2", variable=var, value='12',
                  command=sel)
        R2.place(x=200,y=70,width=85,height=25)
        
        def sel():
            selection = "You selected  "   str(var.get())
            print(selection)


if __name__ == "__main__":
    root = tk.Tk()
    app = App(root)
    root.mainloop()

CodePudding user response:

You haven't defined the variable var anywhere so you can't pass it to your Radiobutton. You need to declare your variable before you can use it.

  • Related