如何自定义ttk.checkbutton颜色?

发布于 2025-02-12 00:35:24 字数 219 浏览 3 评论 0原文

我正在寻找一种更改ttk.checkbutton tickbox的背景颜色(和活动颜色)的方法> “在此处输入图像说明”

最佳的是,盒子的背景颜色应该匹配背景的颜色吗?

style.configure()方法中对此进行修改的命令是什么?

I am looking for a way to change the background color (and the active color) of the tickbox of the ttk.Checkbutton enter image description here

Optimally, the background color of the box should match the background's color?

What is the command to modify this in the style.configure() method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扬花落满肩 2025-02-19 00:35:24

可以使用选项indodatorbackgroundincodatorforeground更改tickbox的颜色。

style.configure("TCheckbutton", indicatorbackground="black", indicatorforeground="white",
                background="black", foreground="white")

要更改活动颜色,您需要使用style.map()而不是style.configure()

style.map("TCheckbutton", background=[("active", "darkgrey")])

请注意,有了一些主题(例如OSX和Windows默认主题),以上命令不会产生任何效果,因为tickbox是用图像创建的。如果是这种情况,则可以更改使用的主题,例如,使用style.theme_use(“ clam”),例如“ alt”或“ clam”。

如果要继续使用不允许更改tickbox颜色的主题,则可以使用自己的tickbox映像:

# create custom tickbox element
img_unticked_box = tk.PhotoImage(file="/path/to/unticked_box_image")
img_ticked_box = tk.PhotoImage(file="/path/to/ticked_box_image")
style.element_create("tickbox", "image", img_unticked_box, ("selected", img_ticked_box))
# replace the checkbutton indicator with the custom tickbox in the Checkbutton's layout
style.layout(
    "TCheckbutton", 
    [('Checkbutton.padding',
      {'sticky': 'nswe',
       'children': [('Checkbutton.tickbox', {'side': 'left', 'sticky':     ''}),
    ('Checkbutton.focus',
         {'side': 'left',
          'sticky': 'w',
      'children': [('Checkbutton.label', {'sticky': 'nswe'})]})]})]
)

我获得了checkbutton pline style.layout的布局(” tcheckbutton”),然后替换'checkbutton.indicator' by 'checkbutton.tickbox'

It is possible to change the colors of the tickbox using the options indicatorbackground and indicatorforeground.

style.configure("TCheckbutton", indicatorbackground="black", indicatorforeground="white",
                background="black", foreground="white")

To change the active colors, you need to use style.map() instead of style.configure().

style.map("TCheckbutton", background=[("active", "darkgrey")])

Note that with some themes (e.g. OSX and Windows default themes), the above commands do not produce any effect because the tickbox is created with an image. If this is the case, you can change the theme to use, for instance, "alt" or "clam", with style.theme_use("clam").

If you want to keep using a theme that does not allow to change the tickbox color, you can use your own tickbox images instead:

# create custom tickbox element
img_unticked_box = tk.PhotoImage(file="/path/to/unticked_box_image")
img_ticked_box = tk.PhotoImage(file="/path/to/ticked_box_image")
style.element_create("tickbox", "image", img_unticked_box, ("selected", img_ticked_box))
# replace the checkbutton indicator with the custom tickbox in the Checkbutton's layout
style.layout(
    "TCheckbutton", 
    [('Checkbutton.padding',
      {'sticky': 'nswe',
       'children': [('Checkbutton.tickbox', {'side': 'left', 'sticky':     ''}),
    ('Checkbutton.focus',
         {'side': 'left',
          'sticky': 'w',
      'children': [('Checkbutton.label', {'sticky': 'nswe'})]})]})]
)

I obtained the layout of the Checkbutton with style.layout("TCheckbutton") and then replaced 'Checkbutton.indicator' by 'Checkbutton.tickbox'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文