Tkinter Label – Center align text
You can center align the text in a Label widget in Tkinter as shown in the following screenshot.
To center align the text in a Label widget in Tkinter, set the anchor parameter to “center”.
The syntax to specify the anchor parameter in a Label widget constructor, so that the text is center aligned, is as shown in the following.
In this tutorial, you will learn how to center align the text in a Label widget in Tkinter, with examples.
1. Center align text in a Label widget
In this example, we shall create a Label widget with some text, and set the alignment of the text to center.
We have set the background color and width, so that we can visually check if the text is actually center aligned or not.
import tkinter as tk # Create the main window window = tk.Tk() window.title("PythonExamples.org") window.geometry("300x200") # Create a label widget label = tk.Label( window, text="Hello World!", anchor="center", width=25, bg="lightgreen" ) # Pack the label widget to display it label.pack() # Run the application window.mainloop()
Run on Windows 10.
Run on MacOS.
In this Python Tkinter tutorial, we learned how to center align text in a Label widget, with examples.