Tkinter之Radiobutton
提供用户选项时会用到,一次只能选择其中一个,不能多选。
示例:
import Tkinter as tk
window = tk.Tk()
window.title('My Window')
window.geometry('500x300')
def print1():
print ("你的选择是:"+v.get())
v = tk.StringVar()
v.set('ss') # 设置默认选中的按钮
#当鼠标选中了某个选项,会把value的值放到变量v中,v再赋值给variable
l1 = tk.Radiobutton(window, text='aaa', variable=v, value='aaa',command=print1)
l1.pack(anchor='w')
l2 = tk.Radiobutton(window, text='ss', variable=v, value='ss',command=print1)
l2.pack(anchor='w')
l3 = tk.Radiobutton(window, text='d', variable=v, value='d',command=print1)
l3.pack(anchor='w')
window.mainloop()
比较实用的代码示例:
import Tkinter as tk
window = tk.Tk()
window.title('My Window')
window.geometry('500x300')
def print1():
print ("你的选择是:"+selectons[v.get()-1][0])
selectons=[('python',1),('C++',2),('C',3)]
v = tk.IntVar()
v.set(3)
for i,j in selectons:
l = tk.Radiobutton(window, text=i, variable=v, value=j,command=print1)
l.pack(anchor='w')
window.mainloop()