Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How do I use Tkinter in Python to create line-wrapped text that fills the width of the window?
Tkinter provides the Text widget to display and edit multiline text. The wrap property controls how text wraps when it exceeds the widget's width. You can wrap by words, characters, or disable wrapping entirely.
Text Wrapping Options
The wrap parameter accepts three values ?
-
WORD− Wraps text at word boundaries (most common) -
CHAR− Wraps text at any character -
NONE− Disables wrapping (adds horizontal scrollbar)
Example: Word Wrapping
Here's how to create a Text widget that wraps text by words ?
import tkinter as tk
# Create the main window
root = tk.Tk()
root.title("Text Wrapping Example")
root.geometry("500x300")
# Sample text
sample_text = """This is a long paragraph that demonstrates text wrapping in Tkinter.
When the text reaches the edge of the widget, it will automatically wrap to the next line
at word boundaries, making it easy to read without horizontal scrolling."""
# Create Text widget with word wrapping
text_widget = tk.Text(root, wrap="word", padx=10, pady=10)
text_widget.insert("1.0", sample_text)
text_widget.pack(fill="both", expand=True)
root.mainloop()
Example: Character Wrapping
Character wrapping breaks lines at any character, useful for displaying code or data ?
import tkinter as tk
root = tk.Tk()
root.title("Character Wrapping")
root.geometry("400x200")
# Text with long words
long_text = "Supercalifragilisticexpialidocious antidisestablishmentarianism pseudopseudohypoparathyroidism"
# Character wrapping
text_widget = tk.Text(root, wrap="char", padx=10, pady=10)
text_widget.insert("1.0", long_text)
text_widget.pack(fill="both", expand=True)
root.mainloop()
Comparison of Wrapping Methods
| Wrap Mode | Breaks At | Best For |
|---|---|---|
WORD |
Word boundaries | Regular text, articles |
CHAR |
Any character | Code, data, long URLs |
NONE |
No wrapping | Preserving original formatting |
Responsive Text Widget
To make text fill the entire window width and resize automatically ?
import tkinter as tk
root = tk.Tk()
root.title("Responsive Text Widget")
root.geometry("600x400")
# Create a responsive text widget
text_widget = tk.Text(root, wrap="word", padx=10, pady=10, bg="white", fg="black")
# Sample content
content = """Welcome to Tkinter Text Wrapping!
This text widget automatically adjusts its content to fill the available width.
Resize the window to see how the text reflows to match the new dimensions.
The fill='both' and expand=True parameters make the widget resize with the window,
while wrap='word' ensures clean line breaks at word boundaries."""
text_widget.insert("1.0", content)
text_widget.pack(fill="both", expand=True, padx=20, pady=20)
root.mainloop()
Conclusion
Use wrap="word" for readable text wrapping in most applications. The fill="both" and expand=True parameters make the Text widget responsive to window resizing, creating a professional user interface.
Advertisements
