Tkinter Label justify Parameter
Tkinter in Label output, if the output is multi-line, you can use the justify parameter to set the output label content to left/center/right in the last line of output, the default is centered output.
Tkinter Label justify Parameter Example#1
Execute multi-line output using the default method and observe that the last line is center-aligned output.
from tkinter import *
root = Tk()
root.title("apidemos.com")
label = Label(root,text="hello, welcome to apidemos.com",
fg="blue",bg="lightyellow",
wraplength=80)
# Use Helvetic glyphs, size 20, bold display.
label.pack() # Packaging and positioning components
# print(type(label)) # <class 'NoneType'>
root.mainloop()
Output:
Tkinter Label justify Parameter Example#2
Performs multi-line output and sets the last line to be left-aligned output.
from tkinter import *
root = Tk()
root.title("apidemos.com")
label = Label(root,text="hello, welcome to apidemos.com",
fg="blue",bg="lightyellow",
wraplength=80,justify="left")
label.pack() # Packaging and positioning components
# print(type(label)) # <class 'NoneType'>
root.mainloop()
Output:
Tkinter Label justify Parameter Example#3
Forced centered output
from tkinter import *
root = Tk()
root.title("apidemos.com")
label = Label(root,text="hello, welcome to apidemos.com",
fg="blue",bg="lightyellow",
wraplength=80,justify="center")
label.pack() # Packaging and positioning components
# print(type(label)) # <class 'NoneType'>
root.mainloop()
Output:
Tkinter Label justify Parameter Example#4
Output to the right
from tkinter import *
root = Tk()
root.title("apidemos.com")
label = Label(root,text="hello, welcome to apidemos.com",
fg="blue",bg="lightyellow",
wraplength=80,justify="right")
label.pack() # Packaging and positioning components
# print(type(label)) # <class 'NoneType'>
root.mainloop()
Output: