pythontkinterlabelttk

Why text alignment is not performed via styles?


I'm trying to organize text alignment in ttk.Label using style in order to reduce the amount of code:

import tkinter as tk
from tkinter import ttk
from tkinter import font

app = tk.Tk()
width = 605
height = 200
x = int((app.winfo_screenwidth() / 2) - (width / 2))
y = int((app.winfo_screenheight() / 2) - (height / 2))
app.geometry(f'{width}x{height}+{x}+{y}')
app.resizable(width=False, height=False)

ttk.Style().configure('question.TLabel', 
                     justify=tk.CENTER, 
                     background='#ffffff', 
                     border=0)
question = font.Font(family='Tahoma', size=14, weight='bold')

ttk.Label(app, style='question.TLabel',
          text='When the view in the widget’s window change,\n'
               'the widget will generate a Tcl command' 
               '\nbased on the scrollcommand.',
          font=question).place(x=90, y=35)

app.mainloop()

RESULT screenshot

But it doesn't work that way. How to fix it?

The ttk.Label documentation says:

If the text you provide contains newline ('\n') characters, this option specifies how each line will be positioned horizontally: tk.LEFT to left-justify; tk.CENTER to center; or tk.RIGHT to right-justify each line. You may also specify this option using a style.

The rest of the parameters specified in the style work. justify=tk.CENTER starts working if moved directly to ttk.Label:

RESULT screenshot


Solution

  • According to the official documentation, justify is not supported in the style. I think the documentation you're using is incorrect.

    ----

    TLabel styling options configurable with ttk::style are:

    -background color
    -compound compound
    -foreground color
    -font font