tkinter单选按钮就是为了这样做而设计的。关键是使用indicatoron选项。当该选项设置为false时,您将看到一个普通按钮,该按钮在选中radiobutton时会凹陷,而在未选中时会升高。在
示例import tkinter as tk
root = tk.Tk()
switch_frame = tk.Frame(root)
switch_frame.pack()
switch_variable = tk.StringVar(value="off")
off_button = tk.Radiobutton(switch_frame, text="Off", variable=switch_variable,
indicatoron=False, value="off", width=8)
low_button = tk.Radiobutton(switch_frame, text="Low", variable=switch_variable,
indicatoron=False, value="low", width=8)
med_button = tk.Radiobutton(switch_frame, text="Medium", variable=switch_variable,
indicatoron=False, value="medium", width=8)
high_button = tk.Radiobutton(switch_frame, text="High", variable=switch_variable,
indicatoron=False, value="high", width=8)
off_button.pack(side="left")
low_button.pack(side="left")
med_button.pack(side="left")
high_button.pack(side="left")
root.mainloop()
上面的代码将显示如下屏幕截图。在截图之前,我点击了“中”按钮: