如何移除 Ttk 筆記本標籤中的虛線?(tkinter)


為了使用選項卡並在應用程式中分離工作流,Tkinter 提供了一個筆記本小部件。我們可以使用筆記本小部件在應用程式中建立選項卡。選項卡可用於將一個特定的框架或事件與另一個隔離。

通常,筆記本小部件可以使用ttk 主題小部件進行配置和設定樣式。因此,為了設定筆記本小部件的樣式,我們在配置中傳遞TNotebookTNotebook.Tab 引數。如果我們單擊某個特定的選項卡,可能會出現一些可以移除的矩形虛線。

示例

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

# Create an instance of tkinter frame
win = Tk()
win.geometry("700x350")

# Create an instance of ttk
style = ttk.Style()

# Define Style for Notebook widget
style.layout("Tab", [('Notebook.tab', {'sticky': 'nswe', 'children':
   [('Notebook.padding', {'side': 'top', 'sticky': 'nswe', 'children':
      [('Notebook.label', {'side': 'top', 'sticky': ''})],
   })],
})]
)

# Use the Defined Style to remove the dashed line from Tabs
style.configure("Tab", focuscolor=style.configure(".")["background"])

# Create a Notebook widget
my_notebook= ttk.Notebook(win)
my_notebook.pack(expand=1,fill=BOTH)

# Creating Tabs
tab1 = ttk.Frame(my_notebook)
my_notebook.add(tab1, text= "Tab 1")
tab2 = ttk.Frame(my_notebook)
my_notebook.add(tab2, text= "Tab2")

# Create a Label in Tabs
Label(tab1, text= "Hello, Howdy?",
   font = ('Helvetica 20 bold')).pack()
Label(tab2, text= "This is a New Tab Context",
   font = ('Helvetica 20 bold')).pack()
win.mainloop()

輸出

執行以上程式碼後,將會顯示包含多個選項卡的視窗。

當在視窗中切換選項卡時,將會顯示它的內容

更新於:08-Jun-2021

828 次瀏覽

開啟您的 職業生涯

透過完成本課程獲得認證

開始
廣告
© . All rights reserved.