And*_*one 6 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()
Run Code Online (Sandbox Code Playgroud)
如果删除style.theme_create()/ ,style.theme_use()则选项卡不再具有圆角,因此程序必须默认添加该样式。
如果没有办法查看主题设置(似乎在文档中找不到它),是否有我可以使用的可能设置列表?专门针对选项卡边框的东西?
关于这一点,有一个类似的问题,是否有 Tkinter/ttk 样式参考?但是,提供的答案中的第一个链接没有列出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')
Run Code Online (Sandbox Code Playgroud)
除了 之外'background',我无法看到上面使用的自定义设置的名称'TNotebook':
style.theme_create('white', settings={
'TNotebook': {'configure':
{'background': background_dark, 'tabmargins': [0, 7, 2, 0], 'padding': [7, 2]}}})
Run Code Online (Sandbox Code Playgroud)
如果我这样做,我会更接近我正在寻找的东西,但仍然不完全是:
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')
Run Code Online (Sandbox Code Playgroud)
循环遍历另一个element_options(Notebook.padding和Notebook.label) 也没有提供我正在寻找的值:(
编辑2
某些样式选项未在任何地方列出,包括Tcl/Tk 文档。
一个例子是'focuscolor'当'TNotebook.Tab'选项卡处于焦点时更改选项卡周围虚线的颜色。
另一个例子是当使用ttk.Style().theme_use('default')或 时.theme_use('classic'),笔记本中的选项卡具有圆角边缘。如果您使用.theme_use('clam')或.theme_use('vista'),Notebook 中的选项卡没有圆角边缘。
我无法在任何文档中找到该样式选项,并且无法通过程序打印它(请参阅上面的编辑部分)。
现在我接受当前的最佳答案(Atlas435)来帮助我得出这个结论。
对于其他遇到此问题的人来说,一个临时解决方案可能是在使用'clam'或创建'vista'一个看起来像选项卡的图片时设置或作为您想要和使用的样式parentttk.Style().theme_create()tab.add(tab1, image=img)
最终的
完整列表可用,请查看 Atlas 435 的答案
终于找到了一个列表,其中包含使用 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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
ttk.Combobox
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
Run Code Online (Sandbox Code Playgroud)
ttk.条目
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
ttk.列表框
.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
Run Code Online (Sandbox Code Playgroud)
菜单
.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
Run Code Online (Sandbox Code Playgroud)
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]
Run Code Online (Sandbox Code Playgroud)
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]
Run Code Online (Sandbox Code Playgroud)
ttk.Panedwindow
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
ttk.单选按钮
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]
Run Code Online (Sandbox Code Playgroud)
ttk.量表
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
Run Code Online (Sandbox Code Playgroud)
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]
Run Code Online (Sandbox Code Playgroud)
ttk.分隔符
ttk::style configure TSeparator -background color
Run Code Online (Sandbox Code Playgroud)
ttk.Sizegrip
ttk::style configure TSizegrip -background color
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
ttk.文本
.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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)