In Tkinter in Python: I have a table with a different label. How can I justify the text that is in the label? Because It is a table and the texts in different labels come together!
from tkinter import *
root=Tk()
a=Label(root,text='Hello World!')
a.pack()
a.place(x=200,y=200)
b=Label(root,text='Bye World')
b.pack()
b.place(x=200,y=100)
I want something for justifying in center some text in label but it is not something that I need plz check this: link
Tkinter Label widget can be aligned using the anchor attributes. In order to calculate the accommodate spacing and alignment of the widget, anchor would help in a better way. Anchor provides several options such as N, W, S, E, NW, NE. SW, SE which can be defined in the pack manager itself.
A label can include any text, and a window can contain many labels (just like any widget can be displayed multiple times in a window). You can easily change/update the Python Tkinter label text with the label text property. Changing the label's text property is another way to change the Tkinter label text.
We can use place() method to set the position of the Tkinter labels.
To right justify text, we use the rjust() function. To center justify text, we use the center() function.
By default, the text in a label is centered in the label. You can control this with the anchor
attribute, and possibly with the justify
attribute. justify
only affects the text when there is more than one line of text in the widget.
For example, to get the text inside a label to be right-aligned you can use anchor="e"
:
a=Label(root,text='Hello World!', anchor="e")
Note, however, this will appear to have no effect if the label is exactly big enough to hold the text. In your specific example, you would need to give each label the same width:
a=Label(..., width=12)
b=Label(..., width=12)
To add on to what Bryan said, LEFT
is the constant you are looking for to correctly format your wrapped text. You can also justify to the RIGHT
or CENTER
(the default).
a=Label(root,text='Hello World!', anchor="e", justify=LEFT)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With