python - tkinter ttk 查看自定义主题设置
问题描述
使用后ttk.Style().theme_create('name', settings={})
是否可以看到该主题的设置?
我问的原因是,当我创建一个新主题并添加ttk.Notebook(root)
到我的代码中时,选项卡有圆角,这是我不想要的。
这是一个例子:
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
root.title("Tab Example")
root.geometry('270x270')
background = '#ffffff'
background_dark = '#f2f2f2'
style = ttk.Style()
style.theme_create('white', settings={
'TLabel': {'configure': {'background': background}},
'TFrame': {'configure': {'background': background}},
'TNotebook': {
'configure': {'background': background_dark, 'tabmargins': [0, 7, 2, 0], 'padding': [7, 2]}},
'TNotebook.Tab': {
'configure': {'background': background_dark, 'padding': [7, 2], 'focuscolor': 'clear'},
'map': {'background': [('selected', background)]}}})
style.theme_use('white')
tab = ttk.Notebook(root)
tab1 = ttk.Frame(tab)
tab2 = ttk.Frame(tab)
tab.add(tab1, text='Tab 1')
tab.add(tab2, text='Tab 2')
tab.pack(expand=1, fill="both")
ttk.Label(tab1, text="example").pack(padx=36, pady=36)
ttk.Label(tab2, text="example 2").pack(padx=36, pady=36)
root.mainloop()
如果删除style.theme_create()
/style.theme_use()
则选项卡不再具有圆角,因此程序必须默认添加该样式。
如果没有办法查看主题设置(似乎无法在文档中找到它),是否有我可以使用的可能设置列表?专门针对标签边框的东西?
在那张纸条上,有一个类似的问题,Is there a Tkinter/ttk style reference?但是,提供的答案中的第一个链接没有列出ttk::notebook下的边框角或边框样式的任何内容,而第二个链接没有响应。
编辑
扩展 Atlas435 的答案,
style_name = ttk.Notebook(None).winfo_class()
# print(style_name) -> 'TNotebook'
print(style.layout('TNotebook')) # -> [('Notebook.client', {'sticky': 'nswe'})]
print(style.element_options('Notebook.client')) # -> ('borderwidth', 'background')
除了'background'
,我看不到我上面使用的自定义设置的名称'TNotebook'
:
style.theme_create('white', settings={
'TNotebook': {'configure':
{'background': background_dark, 'tabmargins': [0, 7, 2, 0], 'padding': [7, 2]}}})
如果我改为这样做,我会更接近我正在寻找的东西,但仍然不完全:
print(style.layout('Tab')) # -> [('Notebook.tab', {'sticky': 'nswe', 'children': [('Notebook.padding', {'sticky': 'nswe', 'children': [('Notebook.label', {'sticky': 'nswe'})]})]})]
print(style.element_options('Notebook.tab')) # -> ('borderwidth', 'background')
循环通过另一个element_options
(Notebook.padding
和Notebook.label
) 也不能提供我正在寻找的值:(
编辑 2
包括Tcl/Tk 文档在内的任何地方都没有列出一些样式选项。
这方面的一个例子是'focuscolor'
,'TNotebook.Tab'
当它处于焦点时,它会改变 Tab 周围虚线的颜色。
另一个例子是使用ttk.Style().theme_use('default')
or时.theme_use('classic')
,Notebook 中的 Tab 具有圆角边缘。如果您使用.theme_use('clam')
or .theme_use('vista')
,则 Notebook 中的选项卡没有圆角边缘。
我无法在任何文档中找到该样式选项,并且无法通过程序打印它(参见上面的编辑部分)。
现在我接受当前的最佳答案(Atlas435)来帮助我得出这个结论。
对于遇到此问题的其他人来说,一个临时解决方案可能是在使用时设置'clam'
或设置'vista'
为,或者创建一个看起来像标签的图片,具有您想要和使用的样式parent
ttk.Style().theme_create()
tab.add(tab1, image=img)
最后
完整列表可用,查看 Atlas 435 的答案
解决方案
ttk 的所有选项的官方列表
终于找到了一个列表,其中包含使用 ttk 进行样式设置的所有选项。 https://wiki.tcl-lang.org/page/Changing+Widget+Colors
ttk.Button
ttk::style configure TButton -background color
ttk::style configure TButton -foreground color
ttk::style configure TButton -font namedfont
ttk::style configure TButton -focuscolor color
ttk::style map TButton -background \
[list active color disabled color readonly color]
ttk::style map TButton -foreground \
[list active color disabled color readonly color]
ttk::style configure TButton -bordercolor color
ttk::style configure TButton -lightcolor color
ttk::style configure TButton -darkcolor color
ttk.Checkbutton
ttk::style configure TCheckbutton -background color
ttk::style configure TCheckbutton -foreground color
ttk::style configure TCheckbutton -font namedfont
ttk::style map TCheckbutton -background \
[list active color disabled color readonly color]
ttk::style map TCheckbutton -foreground \
[list active color disabled color readonly color]
ttk::style configure TCheckbutton -indicatorcolor color
ttk::style map TCheckbutton -indicatorcolor \
[list selected color pressed color]
ttk::style configure TCheckbutton -indicatorrelief relief
ttk::style configure TCheckbutton -indicatormargin padding
ttk::style configure TCheckbutton -indicatordiameter size
ttk::style configure TCheckbutton -borderwidth size
ttk::style configure TCheckbutton -focuscolor color
ttk.组合框
ttk::style configure TCombobox -background color
ttk::style configure TCombobox -foreground color
ttk::style configure TCombobox -fieldbackground color
ttk::style configure TCombobox -darkcolor color
ttk::style configure TCombobox -lightcolor color
ttk::style configure TCombobox -selectbackground color
ttk::style configure TCombobox -selectforeground color
ttk::style configure TCombobox -bordercolor color
ttk::style configure TCombobox -insertcolor color
ttk::style configure TCombobox -insertwidth color
ttk::style configure TCombobox -arrowsize size
ttk::style configure TCombobox -arrowcolor color
ttk::style map TCombobox -background \
[list disabled color readonly color]
ttk::style map TCombobox -foreground \
[list disabled color readonly color]
ttk::style map TCombobox -fieldbackground \
[list disabled color readonly color]
option add *TCombobox*Listbox.background color
option add *TCombobox*Listbox.foreground color
option add *TCombobox*Listbox.selectBackground color
option add *TCombobox*Listbox.selectForeground color
ttk.Entry
ttk::style configure TEntry -background color
ttk::style configure TEntry -foreground color
ttk::style configure TEntry -fieldbackground color
ttk::style configure TEntry -selectbackground color
ttk::style configure TEntry -selectforeground color
ttk::style configure TEntry -bordercolor color
ttk::style configure TEntry -lightcolor color
ttk::style configure TEntry -insertcolor color
ttk::style configure TEntry -insertwidth color
ttk::style map TEntry -background \
[list disabled color readonly color]
ttk::style map TEntry -foreground \
[list disabled color readonly color]
ttk::style map TEntry -fieldbackground \
[list disabled color readonly color]
.entry configure -font namedfont
ttk.Labelframe
ttk::style configure TLabelframe -background color
ttk::style configure TLabelframe -bordercolor color
ttk::style configure TLabelframe -lightcolor color
ttk::style configure TLabelframe -darkcolor color
ttk::style configure TLabelframe.Label -background color
ttk::style configure TLabelframe.Label -foreground color
ttk::style configure TLabelframe.Label -font namedfont
ttk.Listbox
.listbox configure -background color
.listbox configure -foreground color
.listbox configure -disabledforeground color
.listbox configure -selectbackground color
.listbox configure -selectforeground color
.listbox configure -font namedfont
.listbox configure -borderwidth size
.listbox configure -relief relief
.listbox configure -highlightthickness size
.listbox configure -highlightcolor color
.listbox configure -highlightbackground color
菜单
.menu configure -background color
.menu configure -foreground color
.menu configure -activebackground color
.menu configure -activeforeground color
.menu configure -disabledforeground color
.menu configure -font namedfont
.menu configure -selectcolor color
.menu configure -activeborderwidth size
.menu configure -relief relief
ttk.菜单按钮
ttk::style configure TMenubutton -background color
ttk::style configure TMenubutton -foreground color
ttk::style configure TMenubutton -font namedfont
ttk::style configure TMenubutton -arrowcolor color
ttk::style map TMenubutton -background \
[list active color disabled color]
ttk::style map TMenubutton -foreground \
[list active color disabled color]
ttk::style map TMenubutton -arrowcolor \
[list active color disabled color]
ttk.笔记本
ttk::style configure TNotebook -background color
ttk::style configure TNotebook -bordercolor color
ttk::style configure TNotebook -darkcolor color
ttk::style configure TNotebook -lightcolor color
ttk::style configure TNotebook.Tab -background color
ttk::style configure TNotebook.Tab -foreground color
ttk::style configure TNotebook.Tab -bordercolor color
ttk::style configure TNotebook -focuscolor color
ttk::style configure TNotebook -focusthickness size
ttk::style configure TNotebook.Tab -focuscolor color
ttk::style map TNotebook.Tab -background \
[list selected color active color disabled color]
ttk::style map TNotebook.Tab -foreground \
[list selected color active color disabled color]
ttk::style map TNotebook.Tab -lightcolor \
[list selected color {} color]
ttk::style configure TNotebook.Tab -font namedfont
ttk::style map TNotebook.Tab -font \
[list selected namedfont active namedfont disabled namedfont]
ttk.窗格窗口
ttk::style configure TPanedwindow -background color
ttk::style configure Sash -sashthickness 5
ttk::style configure Sash -sashrelief relief
ttk::style configure Sash -sashpad 2
ttk::style configure Sash -handlesize 5
ttk::style configure Sash -handlepad 5
ttk::style configure Sash -background color
ttk::style configure Sash -lightcolor color
ttk::style configure Sash -bordercolor color
ttk.进度条
ttk::style configure TProgressbar -background color
ttk::style configure TProgressbar -troughcolor color
ttk::style configure TProgressbar -lightcolor color
ttk::style configure TProgressbar -darkcolor color
ttk::style configure TProgressbar -bordercolor color
ttk::style configure TProgressbar -barsize size
ttk::style configure TProgressbar -pbarrelief relief
ttk::style configure TProgressbar -borderwidth size
ttk.radiobutton
ttk::style configure TRadiobutton -background color
ttk::style configure TRadiobutton -foreground color
ttk::style configure TRadiobutton -font namedfont
ttk::style map TRadiobutton -background \
[list active color disabled color readonly color]
ttk::style map TRadiobutton -foreground \
[list active color disabled color readonly color]
ttk::style configure TRadiobutton -indicatorcolor color
ttk::style map TRadiobutton -indicatorcolor \
[list selected color pressed color]
ttk.Scale
ttk::style configure TScale -background color
ttk::style configure TScale -troughcolor color
ttk::style map TScale -background \
[list active color]
ttk::style configure TScale -troughrelief relief
ttk::style configure TScale -sliderrelief relief
ttk::style configure TScale -sliderlength size
ttk::style configure TScale -sliderthickness size
ttk::style configure TScale -lightcolor color
ttk::style configure TScale -darkcolor color
ttk::style configure TScale -bordercolor color
ttk.滚动条
ttk::style configure TScrollbar -background color
ttk::style configure TScrollbar -troughcolor color
ttk::style configure TScrollbar -arrowcolor color
ttk::style configure TScrollbar -bordercolor color
ttk::style configure TScrollbar -darkcolor color
ttk::style configure TScrollbar -lightcolor color
ttk::style configure TScrollbar -sliderrelief relief
ttk::style map TScrollbar -background \
[list active color disabled color]
ttk::style map TScrollbar -foreground \
[list active color disabled color]
ttk::style map TScrollbar -arrowcolor \
[list disabled color]
ttk.分隔符
ttk::style configure TSeparator -background color
ttk.Sizegrip
ttk::style configure TSizegrip -background color
ttk.Spinbox
ttk::style configure TSpinbox -background color
ttk::style configure TSpinbox -foreground color
ttk::style configure TSpinbox -darkcolor color
ttk::style configure TSpinbox -lightcolor color
ttk::style configure TSpinbox -fieldbackground color
ttk::style configure TSpinbox -selectbackground color
ttk::style configure TSpinbox -selectforeground color
ttk::style configure TSpinbox -arrowsize size
ttk::style configure TSpinbox -arrowcolor color
ttk::style configure TSpinbox -bordercolor color
ttk::style configure TSpinbox -insertcolor color
ttk::style configure TSpinbox -insertwidth color
ttk::style map TSpinbox -background \
[list active color disabled color readonly color]
ttk::style map TSpinbox -foreground \
[list active color disabled color readonly color]
ttk::style map TSpinbox -fieldbackground \
[list active color disabled color readonly color]
ttk::style map TScrollbar -arrowcolor \
[list disabled color]
.spinbox configure -font namedfont
ttk.Text
.text configure -background color
.text configure -foreground color
.text configure -selectforeground color
.text configure -selectbackground color
.text configure -inactiveselectbackground color
.text configure -insertbackground color
.text configure -font namedfont
.text configure -relief relief
.text configure -borderwidth size
.text configure -highlightcolor color
.text configure -highlightthickness size
.text configure -highlightbackground color
ttk.Treeview
ttk::style configure Treeview -background color
ttk::style configure Treeview -foreground color
ttk::style configure Treeview -font namedfont
ttk::style configure Treeview -fieldbackground color
ttk::style map Treeview -background \
[list selected color]
ttk::style map Treeview -foreground \
[list selected color]
ttk::style configure Treeview -rowheight [expr {[font metrics namedfont -linespace] + 2}]
ttk::style configure Heading -font namedfont
ttk::style configure Heading -background color
ttk::style configure Heading -foreground color
ttk::style configure Heading -padding padding
ttk::style configure Item -foreground color
ttk::style configure Item -focuscolor color
推荐阅读
- cython - Cython:在 pyx 文件中声明一个 PyCapsule_Destructor
- java - 掷 2 个六面骰子 1000 次,结果出人意料
- r - 使用 ggplot2 facet_grid 优化分类变量的绘图 - 仅绘制二分变量的两个值之一的比例
- java - 在 Gradle 和 IntelliJ 中设置语言级别
- maven - 无需使用 mvn 和 Jenkinsfile 编译即可部署工件
- opengl-es - 如何将纹理仅绑定到 OpenGLES 中的一个对象?
- r - 基于小数点前位数的不同舍入规则
- python - Matlab 到 Python 的转换——测试 Lanczos 算法
- c++ - C ++将抽象类型的动态分配对象传递给函数并存储在向量中
- odoo - 检查用户是否点击了复选框