Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

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

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}

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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

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.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...