1

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()
  • Hi, i added a code snippet in the "Question" description – Michael Brunner Dec 04 '21 at 12:13
  • Welcome to Stack Overflow! Please post a [example] of your code; which means that we should be able to copy-paste-run your code, and it reproduces the problem you said it would. Right now, your code will cause some `SyntaxError`s, because the indentation is not correct. See [ask] for more information on asking good questions. – Sylvester Kruin Dec 04 '21 at 14:05
  • The code currently in your question isn't a [mre] in the sense that it is not runnable and things are missing like a definition of `self.nb` and a call to `mainloop()`. – martineau Dec 04 '21 at 19:44
  • Code is now updated in the post. Thanks for your help – Michael Brunner Dec 05 '21 at 16:47
  • When you respond to someone's comment you should put @ followed by their username so a notification is sent to them. Regarding your question: As far as I can tell after looking at the [ttk::notebook manual page](https://www.tcl.tk/man/tcl/TkCmd/ttk_notebook.html), there is no way to specify the alignment (see [TAB OPTIONS](https://www.tcl.tk/man/tcl/TkCmd/ttk_notebook.html#M10)). – martineau Dec 06 '21 at 17:02
  • @martineau Thanks for your feedback. – Michael Brunner Dec 07 '21 at 17:21

2 Answers2

0

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

RBN
  • 1
0

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