tkinter的radiobutton单选扭一例

0 Comments

import tkinter

# 定义单击按钮的回调函数
def selected():
    print(var.get())

root = tkinter.Tk()

var = tkinter.StringVar() # 给单选钮的variable参数设定值

# 下面两个单选纽在单击时都调用同一个函数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()

发表评论