I'm trying to change the size of the tabs in a tkinter Notebook widget, but I haven't been able to find anything that actually changes the width and height of the tabs. Is there not a direct way to change them? Or is there some way to get around it? I'm currently using python 3 if that changes anything.
Asked
Active
Viewed 1.8k times
5
-
You are not able to set a tab width. However you can adjust the tab text with spaces to obtain a width. – Steven Summers Mar 16 '16 at 02:11
-
Yeah, I guess that could work for the width. Still have the issue of height though. I guess changing the size of the text might adjust the tab sizes, but I'm not sure on how to go about doing that. – ScottH Mar 16 '16 at 02:29
3 Answers
13
I think you want padding in the ttk notebook's tabs?
In which case you can use a custom style, which you can adjust to your taste for colors padding etc.
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
style = ttk.Style()
style.theme_create( "MyStyle", parent="alt", settings={
"TNotebook": {"configure": {"tabmargins": [2, 5, 2, 0] } },
"TNotebook.Tab": {"configure": {"padding": [100, 100] },}})
style.theme_use("MyStyle")
a_notebook = ttk.Notebook(root, width=200, height=200)
a_tab = ttk.Frame(a_notebook)
a_notebook.add(a_tab, text = 'This is the first tab')
another_tab = ttk.Frame(a_notebook)
a_notebook.add(another_tab, text = 'This is another tab')
a_notebook.pack(expand=True, fill=tk.BOTH)
tk.Button(root, text='Some Text!').pack(fill=tk.X)
root.mainloop()

Pythonista
- 11,377
- 2
- 31
- 50
4
I did what Pythonista suggested. It worked pretty well, but it modified other widget's behavior too as the theme is applied globally. For example, the text (OK or Cancel) on the "askokcancel" dialog box got left aligned instead of staying in the center. The easiest way I found which just adds padding and doesn't change any other behavior is:
style = ttk.Style()
style.theme_settings("default", {"TNotebook.Tab": {"configure": {"padding": [30, 30]}}})

Nakini
- 772
- 1
- 8
- 19
1
This still didn't work for me. what i need to do is:
style = ttk.Style()
current_theme =style.theme_use()
style.theme_settings(current_theme, {"TNotebook.Tab": {"configure": {"padding": [20, 5]}}})

Avi ba
- 441
- 6
- 10