Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to justify text in label in Tkinter

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

like image 917
A.Elhami Avatar asked May 19 '16 08:05

A.Elhami


People also ask

How do you align labels in python?

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.

Can you change the text of a label in tkinter?

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.

How do I change the position of text in tkinter?

We can use place() method to set the position of the Tkinter labels.

How do you justify in Python?

To right justify text, we use the rjust() function. To center justify text, we use the center() function.


2 Answers

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)
like image 111
Bryan Oakley Avatar answered Oct 06 '22 21:10

Bryan Oakley


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)
like image 25
Vincent Wetzel Avatar answered Oct 06 '22 21:10

Vincent Wetzel