您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python 2.7 Tkinter-确定已选择哪个单选按钮

Python 2.7 Tkinter-确定已选择哪个单选按钮

关键是要确保两个单选按钮共享相同的变量。然后,要知道选择了哪个变量,您只需要获取变量的值即可。

这是一个例子:

import tkinter

# function that is called when you select a certain radio button
def selected():
    print(var.get())


root = tkinter.Tk()

var = tkinter.StringVar() #used to get the 'value' property of a tkinter.Radiobutton

# Note that I added a command to each radio button and a different 'value'
# When you press a radio button, its corresponding 'command' is called.
# In this case, I am linking both radio buttons to the same command: 'selected'

rb1 = tkinter.Radiobutton(text='Radio Button 1', variable=var, 
                          value="Radio 1", command=selected)
rb1.pack()
rb2 = tkinter.Radiobutton(text='Radio Button 2', variable=var, 
                          value="Radio 2", command=selected)
rb2.pack()

root.mainloop()
python 2022/1/1 18:25:52 有181人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶