Python Tkinter-在Radiobutton上获取选择

Python Tkinter-在Radiobutton上获取选择,python,tkinter,radio-button,Python,Tkinter,Radio Button,我需要检索Radiobutton clicked的值,然后使用该值 检索单击的单选按钮的值的方法是什么 设置单选按钮的代码为: radio_uno = Radiobutton(Main,text='Config1', value=1,variable = 1) radio_uno.pack(anchor=W,side=TOP,padx=3,pady=3) radio_due = Radiobutton(Main,text='Config2', value=2,variable =1) radio

我需要检索Radiobutton clicked的值,然后使用该值

检索单击的单选按钮的值的方法是什么

设置单选按钮的代码为:

radio_uno = Radiobutton(Main,text='Config1', value=1,variable = 1)
radio_uno.pack(anchor=W,side=TOP,padx=3,pady=3)
radio_due = Radiobutton(Main,text='Config2', value=2,variable =1)
radio_due.pack(anchor=W,side=TOP,padx=3,pady=3)
radio_tre = Radiobutton(Main,text='Config3', value=3,variable = 1)
radio_tre.pack(anchor=W,side=TOP,padx=3,pady=3)
这是一个解决方案: 创建一个
tk.IntVar()
来跟踪按下的按钮。我假设您从tkinter import*中执行了

radio_var = IntVar()
您需要更改声明按钮的方式:

radio_uno = Radiobutton(Main,text='Config1', value=1,variable = radio_var)
radio_due = Radiobutton(Main,text='Config2', value=2,variable = radio_var)
radio_tre = Radiobutton(Main,text='Config3', value=3,variable = radio_var)
然后使用
get()
方法查看
radio\u var
的值:

which_button_is_selected = radio_var.get()
然后,您可以创建一个
enum
,或者仅创建三个
if
子句,这些子句将根据选择的按钮执行操作:

if(which_button_is_selected == 1):
    #button1 code
elif(which_button_is_selected == 2):
    #button2 code
else(which_button_is_selected == 3):
    #button3 code
这是一个解决方案: 创建一个
tk.IntVar()
来跟踪按下的按钮。我假设您从tkinter import*
中执行了

radio_var = IntVar()
您需要更改声明按钮的方式:

radio_uno = Radiobutton(Main,text='Config1', value=1,variable = radio_var)
radio_due = Radiobutton(Main,text='Config2', value=2,variable = radio_var)
radio_tre = Radiobutton(Main,text='Config3', value=3,variable = radio_var)
然后使用
get()
方法查看
radio\u var
的值:

which_button_is_selected = radio_var.get()
然后,您可以创建一个
enum
,或者仅创建三个
if
子句,这些子句将根据选择的按钮执行操作:

if(which_button_is_selected == 1):
    #button1 code
elif(which_button_is_selected == 2):
    #button2 code
else(which_button_is_selected == 3):
    #button3 code
这不仅仅是一个解决方案,这是正确的解决方案。这不仅仅是一个解决方案,这是正确的解决方案。