轻松获取Radiobutton的值, Radiobutton 添加点击选中响应事件:
点击事件输出选中Radiobutton的值:
def click():
global v
print('您选中的是',v.get())
定义一组 Radiobutton,赋值,并添加command值
v = tk.IntVar()
tk.Radiobutton(root, text='男',variable=v, value=1, command=click).pack()
tk.Radiobutton(root, text='女',variable=v, value=2, command=click).pack()
tk.Radiobutton(root, text='不限',variable=v, value=3, command=click).pack()
输出结果:
完整代码:
import tkinter as tk
root = tk.Tk()
def click():
global v
print('您选中的是',v.get())
v = tk.IntVar()
tk.Radiobutton(root, text='男',variable=v, value=1, command=click).pack()
tk.Radiobutton(root, text='女',variable=v, value=2, command=click).pack()
tk.Radiobutton(root, text='不限',variable=v, value=3, command=click).pack()
root.mainloop()