如何更改选项卡标题框的大小和 ttk 笔记本选项卡的字体? [英] How can I change the size of tab caption box and font of ttk notebook tabs?

查看:73
本文介绍了如何更改选项卡标题框的大小和 ttk 笔记本选项卡的字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 ttk.notebook python 3x 中更改选项卡标题的字体、宽度和高度

I want to change the font, width and height of a tab caption in ttk.notebook python 3x

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

by below code, i can just change the width of tab caption box

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

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

but how i can change the font of "frame 1" and also the height of tab caption box?

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()

推荐答案

基于此answer关于如何自定义Notebook 的 Tab 配置,您可以像这样将字体信息附加到创建的主题中以获得您想要的字体类型:

Based on this answer on how to customise the Notebook's Tab's configuration, you can append the font's info into the created theme like so to get the type of fonts you want:

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 样式.见下面的代码.

The other approach is to directly configure the Notebook's Tab style. See below code.

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') ).前者更改 Notebook 的 Tab 小部件的字体,后者更改 Notebook 的字体.

You have to note a difference between using s.configure('TNotebook.Tab', font=('URW Gothic L','11','bold') ) and s.configure('TNotebook', font=('URW Gothic L','11','bold') ). The former changes the Notebook's Tab widget's font while the latter changes the Notebook's font.

如果您要配置选项卡的许多方面,请使用第一种方法.如果您只想更改 Notebook Tab 的字体,请使用第二种方法.

You use the first approach if you are configuring many aspects of the Tab. You use the 2nd approach if you just want to change the Notebook Tab's font.

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

Using s.configure('.', font=('URW Gothic L','11','bold') ) means all ttk widgets font will be of the same type. Do this if this is what you want.

这篇关于如何更改选项卡标题框的大小和 ttk 笔记本选项卡的字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆