繁体   English   中英

更改 ttk.Notebook 中“标签页眉”的颜色

[英]Change color of "tab header" in ttk.Notebook

问候!

我想更改使用 ttk.Notebook 创建的选项卡标题中显示的颜色。 找了一会儿,发现要改变ttk小部件的样式,我们可以使用ttk。 样式,因为 Notebook 显然没有配置选项来更改其颜色。 但是,我只找到了如何更改 NoteBook 对象的背景和前景,但没有找到如何配置“标签页眉”,其背景为白色(选中时)或灰色(未选中时)。

任何人都可以帮助我吗?

这是我现在拥有的代码,与我正在尝试做的事情有关

import Tkinter as tki
import ttk

...
##Other code. Not relevant here
...

#create tabs and associate the apropriate frames to it
tabs = ttk.Notebook(parent.master)
ttk.Style().configure("TNotebook", background=mainWcolor, foreground='green')   #configure "tabs" background color

paramsFrame = tki.Frame(tabs, bg=mainWcolor)   #frame with control parameters
comsFrame = tki.Frame(tabs, bg=mainWcolor)     #frame with communication parameters.
ssInfoFrame = tki.Frame(tabs, bg=mainWcolor)   #frame with start and stop messages and procedures

tabs.add(paramsFrame, text = "Control")
tabs.add(comsFrame, text = "Communications")
tabs.add(ssInfoFrame, text = "Start & Stop info")
tabs.pack()

您可以尝试创建自定义主题。

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

mygreen = "#d2ffd2"
myred = "#dd0202"

style = ttk.Style()

style.theme_create( "yummy", parent="alt", settings={
        "TNotebook": {"configure": {"tabmargins": [2, 5, 2, 0] } },
        "TNotebook.Tab": {
            "configure": {"padding": [5, 1], "background": mygreen },
            "map":       {"background": [("selected", myred)],
                          "expand": [("selected", [1, 1, 1, 0])] } } } )

style.theme_use("yummy")

note = ttk.Notebook(root)
f1 = ttk.Frame(note, width=300, height=200)
note.add(f1, text = 'First')
f2 = ttk.Frame(note, width=300, height=200)
note.add(f2, text = 'Second')
note.pack(expand=1, fill='both', padx=5, pady=5)

tk.Button(root, text='yummy!').pack(fill='x')

root.mainloop()

编辑

最详细的ttk文档来自tcl/tk world

例如。

http://www.tcl.tk/man/tcl/TkCmd/ttk_notebook.htm

对于一些有用的基于 python 的示例,您可以从http://code.google.com/p/python-ttk/获取 pyttk-samples 包

我已经使用 Oblivion 的答案有一段时间了,但我遇到了一个问题,即打开/保存对话框按钮轮廓消失,并且文本小部件中的 Checkbuttons 从未被选中(即使它们被选中)。 所以,我将主题代码翻译成一些样式配置等来解决问题(它解决了)。 这将让您更改标签栏颜色、标签背景/前景和活动标签背景/前景。 此外,它不会对您选择的其他主题造成问题。 它基本上与翻译过来的主题中的代码相同。 所以,真的,Oblivion 值得大部分功劳。

Style().configure("TNotebook", background=myTabBarColor);
Style().map("TNotebook.Tab", background=[("selected", myActiveTabBackgroundColor)], foreground=[("selected", myActiveTabForegroundColor)]);
Style().configure("TNotebook.Tab", background=myTabBackgroundColor, foreground=myTabForegroundColor);

编辑:显然,此解决方案在 Windows 中不起作用。 我在 Linux(许多版本的 Xubuntu)中对其进行了测试。

我是 python 的初学者,tkinter。 我的应用程序也有这些样式问题。 这适用于 Treeview 风格,现在由 Notebook 检查,它对我使用 Windows 非常有效....theme_use、configure、map。

noteStyle = ttk.Style()
noteStyle.theme_use('default')
noteStyle.configure("TNotebook", background=clr, borderwidth=0)
noteStyle.configure("TNotebook.Tab", background="clr", borderwidth=0)
noteStyle.map("TNotebook", background=[("selected", clr)])

更改选项卡标题的颜色:选择选项卡标题时。 将鼠标悬停在 Tab 标题上时。 删除选项卡标题中文本周围的虚线。

from tkinter import *
from tkinter import ttk

root = Tk()
#background color
color='#21252b'
root.configure(background = color)
root.resizable(False, False)
#Notebook color
sky_color = "sky blue"
gold_color = "gold"
color_tab = "#ccdee0"
#style
style = ttk.Style()
style.theme_create( "beautiful", parent = "alt", settings ={
        "TNotebook": {
            "configure": {"tabmargins": [10, 10, 20, 10], "background":sky_color }},
        "TNotebook.Tab": {
            "configure": {"padding": [30, 15], "background": sky_color, "font":('consolas italic', 14), "borderwidth":[0]},
            "map":       {"background": [("selected", gold_color), ('!active', sky_color), ('active', color_tab)],
                          "expand": [("selected", [1, 1, 1, 0])]}}})
style.theme_use("beautiful")
style.layout("Tab",
                    [('Notebook.tab', {'sticky': 'nswe', 'children':
                        [('Notebook.padding', {'side': 'top', 'sticky': 'nswe', 'children':
                            #[('Notebook.focus', {'side': 'top', 'sticky': 'nswe', 'children':
                                [('Notebook.label', {'side': 'top', 'sticky': ''})],
                            #})],
                        })],
                    })]
                 )
style.configure('TLabel', background = color , foreground = 'white')
style.configure('TFrame', background = color)
#frame
frame_main_notebook = ttk.Frame(root, width = 200, height = 100)
frame_main_notebook.pack()
#note book
main_notebook = ttk.Notebook(frame_main_notebook, width = 200, height = 100)
main_notebook.pack(side = TOP, expand = 1, fill = 'both')
#first tab
frame_one = ttk.Frame(main_notebook, width = 200, height = 100)
frame_one.pack(side = TOP)
main_notebook.add(frame_one, text = '    tab one    ')
ttk.Label(frame_one, text = "this is inside of tab one").pack()
#second tab
frame_two = ttk.Frame(main_notebook, width = 200, height = 100)
frame_two.pack(side = TOP)
ttk.Label(frame_two, text = "this is inside of tab two").pack()
main_notebook.add(frame_two, text = '    tab two    ')
    
root.mainloop()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM