Radiobutton 单选按钮
可以有多项,但只能选中一个
- 创建一个简单的Radiobutton
from tkinter import *
root=Tk()
v = IntVar()
Radiobutton(root,text='ONE',variable=v,value=1).pack(anchor='w')
Radiobutton(root,text='TWO',variable=v,value=2).pack(anchor='w')
Radiobutton(root,text='THREE',variable=v,value=3).pack(anchor='w')
mainloop()
-
三个Radiobutton 是同一个 variable ,但 value 不一样。
一个简单的Radiobutton
- 用 list 生成 Radiobutton
from tkinter import *
root=Tk()
LANGS = [('Python',1),('Perl',2),('Ruby',3),('Lua',4)]
v=IntVar()
v.set(1)
for lang,num in LANGS:
b=Radiobutton(root,text=lang,variable=v,value=num, indicatoron =False)
b.pack(fill='x')
mainloop()
- v.set() 设置默认选中项。
- indicatoron =False 改变显示形式,用按钮形式,也可以设置为 0。
-
indicatoron =Ture 传统的小圆圈加点的形式,也可以设置为 1,不用这个参数时默认这种形式。
默认选中一项
anchor 对齐多个组件的位置
anchor的值有:N,S,E,W,NE,NW,SE,SW
anchor
网友评论