在ttk.Notebook中更改“标签头”的颜色

13 投票
4 回答
35497 浏览
提问于 2025-04-18 02:32

大家好!

我想改变用ttk.Notebook创建的标签页头部的颜色。经过一段时间的搜索,我发现要改变ttk组件的样式,可以使用ttk.Styling,因为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()

提前谢谢大家!

4 个回答

2

我刚开始学习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)])
3

更改标签页标题的颜色:
当你选择一个标签页标题时。
当你把鼠标悬停在标签页标题上时。
去掉标签页标题周围的虚线。

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()
13

我之前一直在用Oblivion的答案,但遇到一个问题,就是打开/保存对话框的按钮轮廓消失了,文本小部件里的复选框也总是看起来没有被选中(即使它们实际上是选中的)。所以,我把主题代码转化成了一些样式配置来解决这个问题(结果真的解决了)。这样你就可以改变标签栏的颜色、标签的背景/前景色以及活动标签的背景/前景色。而且,这样做不会影响你选择的其他主题。其实,这基本上就是把主题里的代码转过来而已。所以,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)上测试过。

22

你可以试着创建一个自定义主题。

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的世界。

比如说:

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

如果你想要一些有用的基于python的例子,可以从这里下载pyttk-samples包:

http://code.google.com/p/python-ttk/

撰写回答