如何更改标签标题框的大小和 ttk 笔记本标签的字体?

How can I change the size of tab caption box and font of ttk notebook tabs?

我想在 ttk.notebook python 3x

中更改选项卡标题的字体、宽度和高度

通过下面的代码,我可以改变标签标题框的宽度

text=f'{"frame 1": ^30s}

但是如何更改 "frame 1" 的字体以及选项卡标题框的高度?

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
notebook = ttk.Notebook(root)

f1 = tk.Frame(notebook, bg='red', width=200, height=200)
f2 = tk.Frame(notebook, bg='blue', width=200, height=200)

notebook.add(f1, text=f'{"frame 1": ^30s}')
notebook.add(f2, text=f'{"frame 2 longer": ^30s}')

notebook.grid(row=0, column=0, sticky="nw")
root.mainloop()

基于这个关于如何自定义笔记本选项卡的配置,您可以将字体信息附加到创建的主题中,这样可以获得您想要的字体类型:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

s = ttk.Style()
s.theme_create( "MyStyle", parent="alt", settings={
        "TNotebook": {"configure": {"tabmargins": [2, 5, 2, 0] } },
        "TNotebook.Tab": {"configure": {"padding": [100, 10],
                                        "font" : ('URW Gothic L', '11', 'bold')},}})
s.theme_use("MyStyle")

notebook = ttk.Notebook(root)

f1 = tk.Frame(notebook, bg='red', width=200, height=200)
f2 = tk.Frame(notebook, bg='blue', width=200, height=200)

notebook.add(f1, text="frame 1" )
notebook.add(f2, text="frame 2 longer" )

notebook.grid(row=0, column=0, sticky="nw")
root.mainloop()

另一种方式是直接配置Notebook的Tab样式。请参阅下面的代码。

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

s = ttk.Style()
s.configure('TNotebook.Tab', font=('URW Gothic L','11','bold') )

notebook = ttk.Notebook(root)

f1 = tk.Frame(notebook, bg='red', width=200, height=200)
f2 = tk.Frame(notebook, bg='blue', width=200, height=200)

notebook.add(f1, text="frame 1" )
notebook.add(f2, text="frame 2 longer" )

notebook.grid(row=0, column=0, sticky="nw")
root.mainloop()

你必须注意使用之间的区别 s.configure('TNotebook.Tab', font=('URW Gothic L','11','bold') )s.configure('TNotebook', font=('URW Gothic L','11','bold') )。前者改变笔记本的标签小部件的字体,而后者改变笔记本的字体。

如果要配置选项卡的多个方面,则使用第一种方法。如果您只想更改笔记本选项卡的字体,请使用第二种方法。

使用s.configure('.', font=('URW Gothic L','11','bold') )意味着所有的ttk widgets 字体都是相同的。如果这是您想要的,请执行此操作。