How do I center the text in a Tkinter Text widget?

TkinterPythonGUI-Programming

Tkinter text widget is a multiline text input widget. It is used to insert, delete, and add textual data in the input field. It provides many built-in functions and attributes in its widget class.

To configure and align the text at the CENTER of a Tkinter Text widget, we can use justify=CENTER property.

Example

# Import the required libraries
from tkinter import *

# Create an instance of tkinter frame or window
win=Tk()

# Set the size of the window
win.geometry("700x350")

text=Text(win)

# Configure the alignment of the text
text.tag_configure("tag_name", justify='center')

# Insert a Demo Text
text.insert("1.0", "How do I center align the text " "in a Tkinter Text widget?")

# Add the tag in the given text
text.tag_add("tag_name", "1.0", "end")
text.pack()

win.mainloop()

Output

Run the above code to display a window with a sample text placed at the center of the Text widget.

raja
Published on 19-Jun-2021 08:30:04
Advertisements