Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何设置圆形的颜色和单选按钮的选择点?_Python_Tkinter - Fatal编程技术网

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

Python 如何设置圆形的颜色和单选按钮的选择点?,python,tkinter,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(para

我正在使用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)
GUI如下所示(黑色箭头由我自己添加):

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


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

一种方法是在单选按钮旁边放置一个标签:

l = Label(root, text="abc", fg="white")
l.pack()
在这种情况下,您还需要将单选按钮的文本更改为“”。这能解决问题吗?

当然,这是可能的

实际上,“小点”是指示器,“圆”是指示器背景

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

要克服这个问题,您应该设置radiobutton的颜色属性(您应该注意该选项!):

其中结果为:

但是,文档声称该选项具有系统相关行为:

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

<>请考虑使用<代码> TTK < /Cord>版本>代码> NaveButks<代码>,用适当的和:

其中结果为:

理论上,您可以为
标签
指示器
指定一组单独的
bg
/
fg
颜色,但问题源于
radiobutton
小部件的实现

如果
打印(style.layout(style\u name))
您将看到以下结构:

[('Radiobutton.padding',
  {'children': [('Radiobutton.indicator', {'side': 'left', 'sticky': ''}),
                ('Radiobutton.focus',
                 {'children': [('Radiobutton.label', {'sticky': 'nswe'})],
                  'side': 'left',
                  'sticky': ''})],
   'sticky': 'nswe'})]
稍后,通过
print(style.element\u options(element)
查找每个元素选项:


请注意,这两个元素都有
'-background'
/
'-foreground'
选项,因此当为整个样式设置该选项时,它们的反应方式相同。换句话说,
标签和
指示器通过设计共享它们的颜色。

这确实解决了问题,但问题在于我单击文本时Radiobutton的一部分设置了指示器,但当我单击标签时,指示器没有设置。因此,如果没有其他方法,这是一个很好的解决方法,但由于Tkinter Radiobutton具有@CommonSense所述的单独设置颜色的属性,因此不应使用这种方法
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()
[('Radiobutton.padding',
  {'children': [('Radiobutton.indicator', {'side': 'left', 'sticky': ''}),
                ('Radiobutton.focus',
                 {'children': [('Radiobutton.label', {'sticky': 'nswe'})],
                  'side': 'left',
                  'sticky': ''})],
   'sticky': 'nswe'})]
# 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')