Michael Brunner
Michael Brunner

Reputation: 11

Is there a way in Tkinter to left-align tabs (Notebook)?

I use ttk.Notebook to create different tabs. The notebook is placed on the left side. The labels in the tabs should not be aligned to the right, but to the left. Can this be configured?

Right-aligned tabs:

right-unaligned tabs

Code Snippet:

import tkinter as tk
from ttkthemes import ThemedTk
import tkinter.ttk  as ttk

class MyApp(ThemedTk):
    def __init__(self, theme="arc"):
        ThemedTk.__init__(self, fonts=True, themebg=True)
        self.set_theme(theme)
        
        self.style = ttk.Style()
        self.style.configure('lefttab.TNotebook', tabposition='wn')
        current_theme =self.style.theme_use()
        self.style.theme_settings(current_theme, {"TNotebook.Tab": {"configure": {'background':'white', "padding": [10, 8]}}}) 
        
        self.nb = ttk.Notebook(self, style='lefttab.TNotebook')
        self.nb.grid(row=0, column=0, sticky='w')
        
        self.page0 = ttk.Frame(self.nb, width=500, height=300)
        self.page1 = ttk.Frame(self.nb, width=500, height=300)
        self.page2 = ttk.Frame(self.nb, width=500, height=300)
        
        self.style.configure("TFrame", background='white')
        
        self.nb.add(self.page0, text='Allgemein', sticky="nsew")
        self.nb.add(self.page1, text='Wand leicht', sticky="nsew")
        self.nb.add(self.page2, text='Wand schwer', sticky="nsew")
        
        self.ok = ttk.Button(self.page1)
        self.ok["text"] = "Button"
        self.ok["command"] = self.handler
        self.ok.grid(row=0, column=0)
        
        
        
    def handler(self):
        print("Button clicked")


if __name__ == "__main__":
    
    app = MyApp()
    app.geometry("500x300")
    app.mainloop()

Upvotes: 1

Views: 1892

Answers (2)

Simão Afonso
Simão Afonso

Reputation: 51

Found a way to do this, by reading the source code. There's an undocumented and buggy option, tabplacement.

Configure the style like this:

# tabposition: first char is position
#              - To place tabs on the right, use `tk.E`
#              other chars are alignment (buggy)
#              - To align tabs to the bottom, use `tk.S`
# tabplacement: this is like an override to `tabposition`, but incomplete and buggy (or just confusing)
#              - The first character should be `tk.N`, but this makes the tabs horizontal on the left! keep it "wrong", but valid (no " ", for example)
#              - The other characters are regular alignments
self.style.configure('lefttab.TNotebook',
                     tabposition=tk.W + tk.N,
                     tabplacement=tk.N + tk.EW)

I think this requires tk 8.6, but I think this is very old now, should be available on Python 3.8 at least, from what I can tell. Check it on tk.TkVersion.

This does something like this (my code, not your example):

Example

Upvotes: 1

RBN
RBN

Reputation: 1

Add some spaces in the text to match the length of longest label.

Upvotes: 0

Related Questions