更改 ttk.Notebook(tkinter)中的“選項卡頭”顏色


選項卡對於多功能 GUI 應用程式非常有用。它有助於將應用程式中的多個任務或流程以選項卡的形式進行隔離。選項卡對於一次處理多個任務非常有用。藉助 Tkinter Notebook 小元件,我們可以在 tkinter 應用程式中建立選項卡。

要配置選項卡的屬性或樣式,我們必須使用 ttk 主題小元件。ttk 主題小元件有助於設定應用程式中存在的任何小元件的樣式。要配置選項卡的背景色,可以使用 ttk 'default' 主題,同時在配置中傳入 'TNotebook.Tab' 作為樣式引數。

示例

# Import the required libraries
from tkinter import *
from tkinter import ttk

# Create an instance of tkinter frame
win = Tk()

# Set the size of the tkinter window
win.geometry("700x350")

# Create an instance of ttk style
s = ttk.Style()
s.theme_use('default')
s.configure('TNotebook.Tab', background="green3")
s.map("TNotebook", background= [("selected", "green3")])

# Create a Notebook widget
nb = ttk.Notebook(win)

# Add a frame for adding a new tab
f1= ttk.Frame(nb, width= 400, height=180)

# Adding the Tab Name
nb.add(f1, text= 'Tkinter-1')
f2 = ttk.Frame(nb, width= 400, height=180)
nb.add(f2, text= "Tkinter-2")

nb.pack(expand= True, fill=BOTH, padx= 5, pady=5)
win.mainloop()

輸出

執行以上程式碼將在視窗中顯示自定義選項卡。您可以透過在配置中新增顏色名稱來修改選項卡的顏色。

更新於:08-Jun-2021

6 千+ 次瀏覽

開啟你的職業生涯

完成課程並獲得認證

開始使用
廣告