如何移除 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 次浏览

开启您的 职业生涯

通过完成本课程获得认证

开始
广告