How to justify text in label in tkinter in Python Need justify in tkinter?


Tkinter Label widgets are used to add images and create text in a particular application. There are various functions and methods available in the library which can be used to style the widgets and its property. In order to justify the text in the label widget, we can use the justify property. It is generally used to justify the position or the alignment of the text such as RIGHT, LEFT, and CENTER.

Example

In this application, we will justify the position of a text label using the justify property.

#Import tkinter library
from tkinter import *
#Create an instance of tkinter frame or window
win= Tk()
#Set the geometry of tkinter frame
win.geometry("750x350")
#Crate a Label widget
label1= Label(win, text="Box1")
label1.pack()
label2= Label(win, text= "\nKeep\nLearning", bd=1, relief= "solid",font= ("Helvetica 20"), justify= RIGHT)
label2.pack()
Label(win, text= "Box2").pack()
label3= Label(win, text="\nLearning\nMakes\nPerfect", bd=1, relief="solid", font=('Helvetica 20'), justify= LEFT)
label3.pack()
win.mainloop()

Output

Running the above code will display a window that contains a label widget with some text. The visual alignment of the text can be defined by the justify property.

Updated on: 22-Apr-2021

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements