[Python] Why text alignment is not performed via styles?

Discussão em 'Python' iniciado por Stack, Março 13, 2025.

  1. Stack

    Stack Membro Participativo

    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 based on the scrollcommand.',
    font=question).place(x=90, y=35)

    app.mainloop()


    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.

    Continue reading...

Compartilhe esta Página