最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Why text alignment is not performed via styles? - Stack Overflow

programmeradmin2浏览0评论

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
  • for this problem you could add screenshot directly in question (not link to external portal) – furas Commented Mar 13 at 19:54
  • I see two solutions (1) you can keep more parameters as dictionary and put them as Label(..., **dictionary_with_parameters) (2) you can create own class MyLabel(ttk.Label) which will always add all settings. – furas Commented Mar 13 at 20:11
  • one of screenshot in this tutorial suggests that justify in style 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:22
  • Thanks for the help! I decided to use justify in the label itself. Less code that way. Special thanks for the tutorial! – KLYSTRON Commented Mar 13 at 22:06
  • According to the official document on styling options, justify is not included. – acw1668 Commented Mar 14 at 1:10
Add a comment  | 

1 Answer 1

Reset to default 3

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

发布评论

评论列表(0)

  1. 暂无评论