簡體   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