在本文中,将介绍如何使用 tkinter Radiobutton 单选按钮小部件,允许用户在许多互斥选项之间进行单项选择。
通常,将单选按钮放在一个集合中。要创建单选按钮,请使用以下构造函数。
selected = tk.StringVar()
r1 = tk.Radiobutton(master, text='Option 1', value='Value 1', variable=selected)
r2 = tk.Radiobutton(master, text='Option 2', value='Value 2', variable=selected)
r3 = tk.Radiobutton(master, text='Option 3', value='value 3', variable=selected)
每个单选按钮都有不同的值 value。但是,同一组中的单选按钮使用相同的变量 variable。
import tkinter as tk
from tkinter.messagebox import showinfo
root = tk.Tk()
root.geometry('600x400+200+200')
root.title('Radiobutton 单选按钮演示')def show_selected():showinfo(title='结果',message=selected_score.get())selected_score = tk.StringVar(value=" ")label = tk.Label(root, text="请选择你的成绩 score 范围:")
label.pack(padx=5, pady=5)frame = tk.Frame(root)
frame.pack()r1 = tk.Radiobutton(frame,text="score>=80", value="优秀", variable=selected_score)
r1.pack(anchor="w")
r2 = tk.Radiobutton(frame,text="80>score>=60", value="及格", variable=selected_score)
r2.pack(anchor="w")
r3 = tk.Radiobutton(frame,text="score<60", value="不及格", variable=selected_score)
r3.pack(anchor="w")button = tk.Button(root, text="获取成绩等级", command=show_selected)
button.pack(padx=5, pady=5)root.mainloop()
如果单选按钮选项比较多,强烈建议使用循环方式结合字典、列表、元组来初始化。同时,也可以不需要任何按钮。每次用户更改 Radiobutton 的状态时,使用参数 command 调用相应的函数。
import tkinter as tk
from tkinter.messagebox import showinfo
root = tk.Tk()
root.geometry('600x400+200+200')
root.title('Radiobutton 单选按钮演示')def show_selected():showinfo(title='结果',message=selected_score.get())selected_score = tk.StringVar(value=" ")label = tk.Label(root, text="请选择你的成绩 score 范围:")
label.pack(padx=5, pady=5)frame = tk.Frame(root)
frame.pack()values = {"score>=80" : "优秀","80>score>=60" : "及格","score<60" : "不及格"} for (text, value) in values.items():r = tk.Radiobutton(frame,text=text, value=value, variable=selected_score, command=show_selected)r.pack(anchor="w")
root.mainloop()
改变 Radiobutton 样式
可以通过设置参数 indicatoron 的值为 0,删除圆形指示器,使用参数 selectcolor,设置选择后单选按钮的颜色。
设置参数 offrelief=“flat”, overrelief=“ridge”,当鼠标悬停在单选按钮上面时,会呈现不同的凸起外观。
import tkinter as tk
from tkinter.messagebox import showinfo
root = tk.Tk()
root.geometry('600x400+200+200')
root.title('Radiobutton 单选按钮演示')def show_selected():showinfo(title='结果',message=selected_score1.get())selected_score1 = tk.StringVar(value=" ")
selected_score2 = tk.StringVar(value=" ")label = tk.Label(root, text="请选择你的成绩 score 范围:")
label.pack(padx=5, pady=5)frame1 = tk.LabelFrame(root, text="无圆形指示器")
frame1.pack(ipadx=10, ipady=10)frame2 = tk.LabelFrame(root, text="动态样式")
frame2.pack(ipadx=10, ipady=10)values = {"score>=80" : "优秀","80>score>=60" : "及格","score<60" : "不及格"} for (text, value) in values.items():r = tk.Radiobutton(frame1,indicatoron=0,width=20,selectcolor="red",text=text,value=value,variable=selected_score1,command=show_selected)r.pack()
for (text, value) in values.items():r = tk.Radiobutton(frame2,indicatoron=0,width=20,selectcolor="red",offrelief="flat", overrelief="ridge",text=text,value=value,variable=selected_score2,command=show_selected)r.pack()
root.mainloop()
替换 Radiobutton 文字为图像
Radiobuttons 的选项不仅仅能够显示文本,还可以显示图像。
同时,利用参数 indicatoron=0 和 一组图像,制作类似工具栏的单选按钮。
import tkinter as tk
from tkinter.messagebox import showinfo
root = tk.Tk()
root.geometry('600x400+200+200')
root.title('Radiobutton 单选按钮演示')selected_score1 = tk.StringVar(value=" ")
selected_score2 = tk.StringVar(value=" ")frame1 = tk.LabelFrame(root, text="图片单选按钮")
frame1.pack(ipadx=10, ipady=10)frame2 = tk.LabelFrame(root, text="图片单选按钮")
frame2.pack(ipadx=10, ipady=10)image1=tk.PhotoImage(file="11.png")
selectimage1=tk.PhotoImage(file="1.png")
image2=tk.PhotoImage(file="22.png")
selectimage2=tk.PhotoImage(file="2.png")
image3=tk.PhotoImage(file="33.png")
selectimage3=tk.PhotoImage(file="3.png")
r = tk.Radiobutton(frame1, image=image1, selectimage=selectimage1, value=1, variable=selected_score1)
r.pack(side=tk.LEFT)
r = tk.Radiobutton(frame1, image=image2, selectimage=selectimage2, value=2, variable=selected_score1)
r.pack(side=tk.LEFT)
r = tk.Radiobutton(frame1, image=image3, selectimage=selectimage3, value=3, variable=selected_score1)
r.pack(side=tk.LEFT)image11=tk.PhotoImage(file="11.png")
selectimage11=tk.PhotoImage(file="1.png")
image22=tk.PhotoImage(file="22.png")
selectimage22=tk.PhotoImage(file="2.png")
image33=tk.PhotoImage(file="33.png")
selectimage33=tk.PhotoImage(file="3.png")
r2 = tk.Radiobutton(frame2, image=image11, selectimage=selectimage11, value=1, variable=selected_score2, indicatoron=0)
r2.pack(side=tk.LEFT)
r2 = tk.Radiobutton(frame2, image=image22, selectimage=selectimage22, value=2, variable=selected_score2, indicatoron=0)
r2.pack(side=tk.LEFT)
r2 = tk.Radiobutton(frame2, image=image33, selectimage=selectimage33, value=3, variable=selected_score2, indicatoron=0)
r2.pack(side=tk.LEFT)root.mainloop()
Radiobutton 方法
select() 此方法用于选择单选按钮。
deselect() 此方法用于取消选择单选按钮。
import tkinter as tk
from tkinter.messagebox import showinfo
root = tk.Tk()
root.geometry('600x400+200+200')
root.title('Radiobutton 单选按钮演示')def show_selected():showinfo(title='结果',message=selected_score.get())selected_score = tk.StringVar(value=" ")label = tk.Label(root, text="请选择你的成绩 score 范围:")
label.pack(padx=5, pady=5)frame = tk.Frame(root)
frame.pack()r1 = tk.Radiobutton(frame,text="option 1", value=1, variable=selected_score)
r1.pack(anchor="w")
r2 = tk.Radiobutton(frame,text="option 2", value=2, variable=selected_score)
r2.pack(anchor="w")
r3 = tk.Radiobutton(frame,text="option 3", value=3, variable=selected_score)
r3.pack(anchor="w")button1 = tk.Button(root, text="选择 option 1", command=lambda: r1.select())
button1.pack(padx=5, pady=5)
button2 = tk.Button(root, text="选择 option 2", command=lambda: r2.select())
button2.pack(padx=5, pady=5)
button3 = tk.Button(root, text="取消 option 3", command=lambda: r3.deselect())
button3.pack(padx=5, pady=5)root.mainloop()