【问题标题】:Ttk Theme SettingsTtk 主题设置
【发布时间】:2015-10-06 18:33:50
【问题描述】:

尝试更改 Checkbutton 的样式,我只是好奇是否可以更改框本身的大小?

这是我目前所拥有的。在配置部分尝试了“高度”和“宽度”,但似乎没有成功。

    s = ttk.Style()
    s.theme_use('default')
    s.configure("cbutton.TCheckbutton", foreground='#ebebeb', background='#5c5c5c', font=("arial", 14))

    s.theme_settings("default", 
       {"TCheckbutton": {
           "configure": {"padding": 5},
               "map": {
                   "background": [("active", "#5C5C5C"),("!disabled", "#5C5C5C")],
                       "fieldbackground": [("!disabled", "#5C5C5C")],
                   "foreground": [("focus", "lightgray"),("!disabled", "lightgray")], "indicatorcolor": [('selected','#9ac947'),('pressed','#9ac947')]
              }
          }
       })

这可能吗?

谢谢!

【问题讨论】:

  • 我不确定,但我不这么认为。我浏览了在线文档和“Tcl and the Tk Toolkit”第二版,但找不到任何看起来像调整复选框本身大小的东西。
  • 正如您在 [文档]tcl.tk/man/tcl8.5/TkCmd/ttk_checkbutton.htm#M10 中所读到的,宽度和高度用于指定可用于 Checkbutton 小部件文本的空间。不幸的是,您无法对 Checkbutton 样式进行任何操作来直接放大它。您可以通过自己创建一个新的 Checkbutton 小部件(例如基于 Frame 或 Button 小部件)来模仿它,并将背景图像设置为空的或选中的 checkbutton 图像来实现这一点。
  • 很高兴知道——谢谢!

标签: python python-3.x tkinter ttk


【解决方案1】:

ttk中的指标元素支持backgroundborderwidthindicatorcolorindicatorreliefindicatordiameterindicatormargin。这些都设置为主题配置值,使用style.configure() 作为小部件样式。您可以通过更改indicatordiameter 来更改 Tk 绘制主题的指示器元素的大小。例如:

style = ttk.Style()
style.layout('Custom.Checkbutton', style.layout('TCheckbutton'))
style.map('Custom.Checkbutton', **style.map('TCheckbutton'))
style.configure('Custom.Checkbutton', **style.configure('TCheckbutton'))
style.configure('Custom.Checkbutton', indicatordiameter='24')

将标准检查按钮样式 (TCheckbutton) 复制到新样式中,然后覆盖自定义样式的指示器大小。

请注意,对于使用不是由 Tk 绘制的指示元素的主题,这将不受支持。例如,Windows 主题的指示器元素由 Visual Styles API 提供,其大小由系统定义的主题引擎确定。如果您必须启用这种主题自定义,则可以将“默认”主题中的指示器元素导入 Windows 主题,但代价是随着 UI 外观和感觉开始在该平台上使应用程序的某些部分看起来很奇怪不匹配。

【讨论】:

    猜你喜欢
    • 2021-12-21
    • 2016-02-20
    • 2018-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多