Center Align Text in Text Widget in Tkinter Python

In Tkinter, the Text widget provides a multi-line text area where users can enter and display text. By default, the text is left-aligned, but we can center align the text using the tag_configure method with the justify option.

To center align text in a Tkinter Text widget, we:

  • Use the tag_configure method to define a text tag with justify="center".
  • Insert the text and apply the tag using text_widget.insert("1.0", "Your text here") and text_widget.tag_add("center", "1.0", "end").

In this tutorial, we will go through multiple examples of center-aligning text inside a Tkinter Text Widget.

Examples

1. Center Aligning Single-Line Text

In this example, we create a Tkinter Text widget and align the text to the center.

We define a Text widget inside the main Tkinter window and apply a tag named center using tag_configure with justify="center". Then, we insert text using insert() and apply the tag using tag_add() to ensure the text is center-aligned.

main.py

</>
Copy
import tkinter as tk

# Create main window
root = tk.Tk()
root.title("Tkinter Center Align Text - tutorialkart.com")
root.geometry("400x200")

# Create Text widget
text_widget = tk.Text(root, height=3, width=40)
text_widget.pack(pady=20)

# Configure tag to center align text
text_widget.tag_configure("center", justify="center")

# Insert text and apply center alignment
text_widget.insert("1.0", "Welcome to Tkinter!")
text_widget.tag_add("center", "1.0", "end")

root.mainloop()

Output in Windows:

The text ‘Welcome to Tkinter!’ is center aligned in the Text Widget.

text widget with center aligned text

2. Center Aligning Multiple Lines of Text in Text Widget

In this example, we center-align multiple lines of text inside a Tkinter Text widget.

We create a Text widget, define a tag named center using tag_configure with justify="center", and insert multiple lines of text. The tag_add() method is used to apply the tag across all inserted text, ensuring that every line is centered.

main.py

</>
Copy
import tkinter as tk

# Create main window
root = tk.Tk()
root.title("Tkinter Center Align Text - tutorialkart.com")
root.geometry("400x200")

# Create Text widget
text_widget = tk.Text(root, height=5, width=40)
text_widget.pack(pady=20)

# Configure tag to center align text
text_widget.tag_configure("center", justify="center")

# Insert multiple lines of text
text_widget.insert("1.0", "Welcome to Tkinter!\n")
text_widget.insert("2.0", "This text is centered.\n")
text_widget.insert("3.0", "You can add multiple lines.")

# Apply center alignment to all lines
text_widget.tag_add("center", "1.0", "end")

root.mainloop()

Output in Windows:

The text widget has multiple lines, where all the lines are center aligned.

Text Widget with three lines, all center aligned

Conclusion

In this tutorial, we explored how to center align text in a Tkinter Text widget using the tag_configure method with the justify="center" option. We applied this technique to:

  • A single-line text inside a Text widget.
  • Multiple lines of text while maintaining proper center alignment.