更改ttk.Noteb中“tab header”的颜色

2024-05-12 17:29:37 发布

您现在位置:Python中文网/ 问答频道 /正文

格雷廷斯!

我想更改使用ttk.Notebook创建的选项卡标题中显示的颜色。 经过一段时间的搜索,我发现要改变ttk小部件的样式,我们可以使用ttk。样式,因为笔记本显然没有配置选项来更改其颜色。但是,我只找到了如何更改笔记本对象的背景和前景,而没有找到如何配置“tab header”,其背景为白色(选中时)或灰色(未选中时)。

有人能帮我吗?

这是我现在的代码,和我要做的有关

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

提前谢谢。


Tags: andtextadd颜色with笔记本样式frame
2条回答

我已经使用Oblivion的答案有一段时间了,但是我遇到了一个问题,打开/保存对话框按钮的轮廓消失了,文本小部件中的checkbutton似乎从未被选中过(即使它们被选中)。所以,我将主题代码转换成一些样式配置,这样就解决了问题(它解决了问题)。这将允许您更改选项卡栏颜色、选项卡背景/前景和活动选项卡背景/前景。另外,它不会对你选择的主题的其他部分造成问题。从本质上讲,这是从主题翻译过来的相同代码。所以,真的,遗忘是最值得称赞的。

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的许多版本)中测试了它。

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

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/

相关问题 更多 >