I'm trying to anize 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
I'm trying to anize 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
Share Improve this question edited Mar 13 at 22:00 KLYSTRON asked Mar 13 at 17:36 KLYSTRONKLYSTRON 134 bronze badges 5 |1 Answer
Reset to default 3According 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
Label(..., **dictionary_with_parameters)
(2) you can create ownclass MyLabel(ttk.Label)
which will always add all settings. – furas Commented Mar 13 at 20:11justify
instyle
works on Windows (at least on old version) but it doesn't work for me on Linux. Maybe it depens on system. – furas Commented Mar 13 at 20:22justify
is not included. – acw1668 Commented Mar 14 at 1:10