2

Is it possible to change width of notebooks tabs in tkinter without just entering space in the name of the tab? I have tried to put width but I didnt succeed. Is there any option like tabwidht or something like that, so that I can have fixed size of tab?

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

style = ttk.Style(root)
style.configure('lefttab.TNotebook', tabposition='wn',width=80)

notebook = ttk.Notebook(root, style='lefttab.TNotebook')

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()
taga
  • 3,537
  • 13
  • 53
  • 119

2 Answers2

4

Use string formatting, when defining the text of your tabs:

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": ^20s}')
notebook.add(f2, text=f'{"frame 2 longer": ^20s}')

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

Instead of '20' use a size of your choice. Be careful to use the correct quote characters:

f'{"something": ^20s}'

or the other way round:

f"{'something': ^20s}"
rengel
  • 453
  • 1
  • 5
  • 12
  • As an afterthought: Of course, this is just another way to add space to the text, so it doesn't quite answer your question. But then: it works. – rengel Aug 29 '18 at 15:19
  • Do you mind explaining why this works/what you're doing? I'm a bit lost... – wscheib Mar 19 '19 at 05:06
  • notebook treats the whole string (including trailing spaces) passed in as the title of the tab. – rengel Apr 18 '19 at 05:24
0

You can use a ttk custom style to configure the size:

import tkinter, tkinter.ttk

#Setup a custom style
notebookStyle = ttk.Style()
notebookStyle.configure('Custom.TNotebook.Tab', padding=[30,4])
            
tabControl = ttk.Notebook(parent, style = "Custom.TNotebook")

As explained in this answer.

Wudfulstan
  • 129
  • 11