如何设置单选按钮的圆圈颜色和选择点?

Col*_*ver 7 python tkinter

我正在使用Python tkinter。我可以通过设置Radiobutton的属性来自定义背景颜色(使用“bg”属性)和字符颜色(使用“fg”属性)。现在我需要:

  • 红色背景和
  • 白色字符

所以我创建了一个像这样的单选按钮:

common_bg = '#' + ''.join([hex(x)[2:].zfill(2) for x in (181, 26, 18)])  # RGB in dec
common_fg = '#ffffff'  # pure white
Radiobutton(paraent_frame, text='abc', variable=radio_value, value=2,
            command=on_click_level_radio, bg=common_bg, fg=common_fg)
Run Code Online (Sandbox Code Playgroud)

GUI看起来像这样(黑色箭头是我自己添加的):

在此输入图像描述

但问题是,单选按钮圆圈的背景是白色的。当用于指示是否选择收音机的“小点”默认为黑色时,效果很好。当我将“fg”属性设置为白色时,“小点”也变成白色,从而无法区分是否选择了无线电。

所以我想知道是否有一种方法可以单独设置单选按钮的“圆圈”或“小点”的颜色。

Com*_*nse 5

当然,这是可能的。

实际上,“小点”就是指标,“圆”就是指标背景

在您的情况下,指示器从foreground/activeforeground选项继承其颜色(这些是纯白色),并且指示器背景默认为纯白色。

为了克服这个问题,您应该设置单选按钮的颜色属性(您应该注意到该selectcolor选项!):

try:
    import tkinter as tk
except ImportError:
    import Tkinter as tk

root = tk.Tk()

common_bg = '#' + ''.join([hex(x)[2:].zfill(2) for x in (181, 26, 18)])  # RGB in dec
common_fg = '#ffffff'  # pure white

rad_button = tk.Radiobutton(root, text='abc', value=2, fg=common_fg, bg=common_bg,
                            activebackground=common_bg, activeforeground=common_fg, selectcolor=common_bg)

rad_button.pack(expand=True, fill='both')
root.mainloop()
Run Code Online (Sandbox Code Playgroud)

结果是:

结果

但是,文档声称此选项具有依赖于系统的行为:

指定选择按钮时要使用的背景颜色。如果 -indicatoron 为 true,则颜色应用于指示器。在 Windows 下,无论选择状态如何,此颜色都用作指示器的背景。如果 -indicatoron 为 false,则每当选择小部件时,都会使用此颜色作为整个小部件的背景,代替 -background 或 -activeBackground。如果指定为空字符串,则选择小部件时不会使用特殊颜色进行显示。

因此,请考虑使用具有适当主题选项ttk的版本:radiobutton

try:
    import tkinter as tk
    import tkinter.ttk as ttk
except ImportError:
    import Tkinter as tk
    import ttk

root = tk.Tk()
style = ttk.Style(root)

#    try also the 'clam' theme
style.theme_use('alt')

common_bg = '#' + ''.join([hex(x)[2:].zfill(2) for x in (181, 26, 18)])  # RGB in dec
#    alternatively use the "more red" version of the common_bg as the indicatorcolor
#    sel_bg = '#' + ''.join([hex(x)[2:].zfill(2) for x in (221, 26, 18)])
common_fg = '#ffffff'  # pure white

rad_button = ttk.Radiobutton(root, text='abc')
rad_button.pack(expand=True, fill='both')

style_name = rad_button.winfo_class()
style.configure(style_name, foreground=common_fg, background=common_bg, indicatorcolor=common_bg)

style.map(style_name,
          foreground = [('disabled', common_fg),
                      ('pressed', common_fg),
                      ('active', common_fg)],
          background = [('disabled', common_bg),
                      ('pressed', '!focus', common_bg),
                      ('active', common_bg)],
          indicatorcolor=[('selected', common_bg),
                          ('pressed', common_bg)]

          )
root.mainloop()
Run Code Online (Sandbox Code Playgroud)

结果是:

结果

理论上,您可以为和指定单独的 a bg/fg颜色集,但问题源于小部件的实现。labelindicatorradiobutton

如果print(style.layout(style_name))你会看到这个结构:

[('Radiobutton.padding',
  {'children': [('Radiobutton.indicator', {'side': 'left', 'sticky': ''}),
                ('Radiobutton.focus',
                 {'children': [('Radiobutton.label', {'sticky': 'nswe'})],
                  'side': 'left',
                  'sticky': ''})],
   'sticky': 'nswe'})]
Run Code Online (Sandbox Code Playgroud)

稍后,通过以下方式查找每个元素选项print(style.element_options(element)

# result of print(style.element_options('Radiobutton.indicator'))
('-background', '-foreground', '-indicatorcolor', '-lightcolor', '-shadecolor', '-bordercolor', '-indicatormargin')

# result of print(style.element_options('Radiobutton.label'))
('-compound', '-space', '-text', '-font', '-foreground', '-underline', '-width', '-anchor', '-justify', '-wraplength', '-embossed', '-image', '-stipple', '-background')
Run Code Online (Sandbox Code Playgroud)

请注意,两个元素都有'-background'/'-foreground'选项,因此当为整个样式设置该选项时,它们都会以相同的方式做出反应。换句话说,通过设计label分享indicator他们的颜色。