ttk 按钮边框样式

use*_*450 6 python linux tkinter tcl ttk

我正在尝试弄清楚如何在 Linux 上使用 Python ( ttk.Button ) 设置 ttk 按钮的样式,现在我专注于边框。所有内置主题(altclamclassicdefault)似乎都使用Button.border元素,但它们列出了不同的选项,并且不一定支持某些列出的选项。

  • alt列出-background-bordercolor-default-borderwidth-relief。但是,-bordercolor会被忽略。

  • clam列出-bordercolor-lightcolor-darkcolor-relief-borderwidth。但是,-borderwidth会被忽略。

  • 经典列表-background-borderwidth-relief-default

  • 默认列表-background-borderwidth-relief

跨主题的按钮边框

为什么对于Button.border元素,只有clam支持-bordercolor-lightcolor-darkcolor,而只有其他元素支持-borderwidth?难道所有主题不都应该支持所有这些选项吗?

我绝不精通阅读 Tcl,但我没有在 ttk 主题文件(altTheme.tclclamTheme.tclclassicTheme.tcldefaults.tcl)中看到任何表明Button.border元素是自定义的或任何对它的引用。TButton样式只有一些配置映射调用。


这是我用来查看上面结果的代码:

import pprint
import tkinter as tk
import tkinter.ttk as ttk

def theme_changed(theme):
    style.theme_use(theme)

    print(theme)
    pprint.pprint(style.layout('TButton'))
    pprint.pprint(style.element_options('Button.border'))

    style.configure(
        'Custom.TButton',
        background='#FFFFFF', # White
        bordercolor='#00FF00', # Green
        lightcolor='#FF0000', # Red
        darkcolor='#0000FF', # Blue
        borderwidth=4,
        foreground='#00FFFF', # Cyan
    )

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

combo = ttk.Combobox(root, values=sorted(style.theme_names()), state='readonly')
combo.set(style.theme_use())
combo.bind('<<ComboboxSelected>>', lambda _e: theme_changed(combo.get()))
combo.pack()

theme_changed(style.theme_use())

button = ttk.Button(root, text="Normal Button")
button.pack()

button = ttk.Button(root, style="Custom.TButton", text="Custom Button")
button.pack()

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