更改“标签标题"的颜色在 ttk.Notebook [英] Change color of "tab header" in ttk.Notebook
问题描述
您好!
我想更改使用 ttk.Notebook 创建的选项卡标题中显示的颜色.搜索了一段时间后发现,要改变ttk小部件的样式,我们可以使用ttk.样式,因为 Notebook 显然没有更改其颜色的配置选项.但是,我只找到了如何更改 NoteBook 对象的背景和前景,但没有找到如何配置选项卡标题",其背景为白色(选中时)或灰色(未选中时).
I want to change the color displayed in a tab header, created using ttk.Notebook. After search for a while I've found that to change the style of ttk widgets, we can use ttk. Styling, because Notebook apparently do not have configuration options to change its colors. However, I only found how to change the background and the foreground of a NoteBook object, but not how to configure the "tab header", whose background is either white (when selected) or grey (when not selected).
有人可以帮我解决这个问题吗?
Anybody can help me with this?
这是我现在拥有的代码,与我正在尝试做的事情相关
This is the code that I have for now, related with what I'm trying to do
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()
提前致谢.
推荐答案
您可以尝试创建自定义主题.
You can try creating a custom theme.
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世界
The most detailed ttk documentation is from the tcl/tk world
例如
http://www.tcl.tk/man/tcl/TkCmd/ttk_notebook.htm
对于一些有用的基于 python 的示例,您可以获取 pyttk-samples 包来自 http://code.google.com/p/python-ttk/
For some useful python-based examples, you can grab the pyttk-samples package from http://code.google.com/p/python-ttk/
这篇关于更改“标签标题"的颜色在 ttk.Notebook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!