Radiobutton组件
Radiobutton组件跟Checkbutton组件的用法基本一致,唯一不同的是Radiobutton实现的是“单选”的效果。
要实现这种互斥的效果,同组内的所有Radiobutton只能共享一个variable选项,并且需要设置不同的value选项值。
注释掉的也是一种方法
import tkinter as tk
root = tk.Tk()
v=tk.IntVar()
#tk.Radiobutton(root,text="One",variable=v,value=1).pack(anchor=tk.W)
#tk.Radiobutton(root,text="Two",variable=v,value=2).pack(anchor=tk.W)
#tk.Radiobutton(root,text="Three",variable=v,value=3).pack(anchor=tk.W)
texts=["One","Two","Three"]
i=1
for text in texts:
tk.Radiobutton(root,text=text,variable=v,value=i).pack(anchor=tk.W)
i+=1
tk.mainloop()
------------------------------------------------------------------------
import tkinter as tk
root = tk.Tk()
v=tk.IntVar()
v.set(1)
#set的括号里是1的话,一开始的默认选项在1;括号里是2的话,默认选项在2;如果超过了4,则没有默认选项
langs = [("One",1),("Two",2),("Three",3)]
for lang,num in langs:
tk.Radiobutton(root,text=lang,variable=v,value=num).pack(anchor=tk.W)
tk.mainloop()
也可以把前面的小圆圈换成按钮的形式
#在for循环进行改动
for text in texts:
tk.Radiobutton(root,text=text,variable=v,value=i,indicatoron&#