单选按钮的Tkinter反转颜色

时间:2019-01-03 10:23:30

标签: python tkinter radio-button

在选择单选按钮时尝试使其颜色反转。当前,当未选择按钮时,背景为灰色,前景为白色。选择后,背景变为白色,但前景也保持白色。我希望能够在选择前景时更改其颜色。

import tkinter as tk
root = tk.Tk()
v = tk.IntVar()
v.set(0)

periods = [
    ("Current"), 
    ("-1"), 
    ("-2"), 
    ("1 Week"),
    ("2 Weeks"), 
    ("1 Month"), 
    ("3 Months"), 
]
def ShowChoice():
    print (v.get())

for val, period in enumerate(periods):
    tk.Radiobutton(root, 
              text=period, 
              indicatoron =0,
              padx=20,
              variable=v, 
              command=ShowChoice, 
              value=val,
              background='gray15', 
              foreground='snow',).pack(side=tk.LEFT)
root.mainloop()

这是当前按钮的外观:

White bg on selected radiobutton

预先感谢

1 个答案:

答案 0 :(得分:2)

就这样

selectcolor='gray25'

单选按钮的选项现在看起来像这样:

for val, period in enumerate(periods):
    tk.Radiobutton(root, 
              text=period, 
              indicatoron =0,
              padx=20,
              variable=v, 
              command=ShowChoice, 
              value=val,
              background='gray15', 
              foreground='snow',
              selectcolor='gray25').pack(side=tk.LEFT)
root.mainloop()

结果是这样的: enter image description here