Jus*_*ues 5 python tkinter ttk
Gretings!
我想更改使用ttk.Notebook创建的选项卡标题中显示的颜色.在搜索了一段时间之后,我发现要改变ttk小部件的样式,我们可以使用ttk.样式,因为笔记本显然没有配置选项来改变它的颜色.但是,我只找到了如何更改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()
Run Code Online (Sandbox Code Playgroud)
提前致谢.
Shu*_*ule 10
我已经使用 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);
Run Code Online (Sandbox Code Playgroud)
编辑:显然,此解决方案在 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()
Run Code Online (Sandbox Code Playgroud)
编辑
最详细的ttk文档来自tcl/tk世界
例如.
http://www.tcl.tk/man/tcl/TkCmd/ttk_notebook.htm
对于一些有用的基于python的例子,你可以从抢pyttk样本包http://code.google.com/p/python-ttk/