Tkinter Text Widget Python

Tkinter Text Widget Thumbnail

Tkinter Text Widget 

How to Create a Multi-Line Text Editor in Python

The Tkinter Text widget is used when a Python GUI application needs to accept or display multiple lines of text.

Unlike the Entry widget, which is mainly designed for single-line input, the Text widget can contain paragraphs, notes, source code, messages, and other larger blocks of text.

In this tutorial: You will learn how to create a Tkinter Text widget, insert and read text, clear its contents, set its size, customize fonts and colors, work with selected text, and build a simple multi-line text editor.

What Is the Tkinter Text Widget?

The Text widget provides a multi-line text area in a Tkinter application.

The basic syntax is:

tk.Text(parent, options)

For example:

text_area = tk.Text(window)

The widget can then be displayed using a geometry manager such as pack() or grid().

Create a Basic Text Widget

Let's create a simple multi-line text area.

import tkinter as tk



window = tk.Tk()



window.title("Text Widget")

window.geometry("600x400")



text_area = tk.Text(window)



text_area.pack(

    padx=20,

    pady=20,

    fill="both",

    expand=True

)



window.mainloop()
Create a Basic Text Widget Screenshot

You can now click inside the text area and type multiple lines.

Set Text Widget Size

The width and height options can be used to define the approximate size of the Text widget.

text_area = tk.Text(

    window,

    width=50,

    height=15

)

The dimensions are generally related to text characters and lines rather than being direct pixel measurements.

Tip: For a window that should resize naturally, combining fill="both" and expand=True with pack() is often useful.

Insert Text

The Text widget provides the insert() method for adding text.

text_area.insert("1.0", "Hello Tkinter!")

The first argument, "1.0", means:

  • 1 = line 1
  • 0 = character position 0

For example:

import tkinter as tk

window = tk.Tk()

window.geometry("600x400")



text_area = tk.Text(window)



text_area.pack(

    padx=20,

    pady=20,

    fill="both",

    expand=True

)



text_area.insert(

    "1.0",

    "Welcome to Python Tkinter!\n"

    "This is a multi-line Text widget."

)



window.mainloop()
Insert Text Screenshot

Read Text

Use the get() method to retrieve text from a Text widget.

content = text_area.get("1.0", tk.END)

print(content)

The first index specifies where reading starts and the second specifies where it ends.

A very common pattern is:

content = text_area.get("1.0", tk.END)
Important: A Text widget normally includes a trailing newline when reading all content with tk.END. If you need to remove extra whitespace, you can use strip().
content = text_area.get(

    "1.0",

    tk.END

).strip()

Delete Text

The delete() method removes text between two indexes.

text_area.delete("1.0", tk.END)

This removes all text from the widget.

Clear All Text

A Clear button can use the same operation:

def clear_text():

    text_area.delete("1.0", tk.END)

Complete example:

import tkinter as tk

def clear_text():

    text_area.delete("1.0", tk.END)

window = tk.Tk()

window.title("Clear Text")

window.geometry("600x400")

text_area = tk.Text(window)

text_area.pack(

    padx=20,

    pady=20,

    fill="both",

    expand=True

)

clear_button = tk.Button(

    window,

    text="Clear",

    command=clear_text

)

clear_button.pack(pady=10)

window.mainloop()

Add Default Text

You can insert initial content when the application starts.

text_area.insert(

    "1.0",

    "Write your notes here..."

)

You can also insert multiple lines:

text_area.insert(

    "1.0",

    "Python\n"

    "Tkinter\n"

    "GUI Development"

)

Change Text Font

The font option controls the appearance of text.

text_area = tk.Text(

    window,

    font=("Arial", 14)

)

For source-code editors, a monospace font can be useful:

text_area = tk.Text(

    window,

    font=("Consolas", 12)

)

You can also specify bold text:

font=("Arial", 14, "bold")

Change Colors

You can customize the Text widget's background and foreground colors.

text_area = tk.Text(

    window,

    bg="#ffffff",

    fg="#222222"

)

You can also customize the insertion cursor:

text_area = tk.Text(

    window,

    insertbackground="black"

)

Combining these options can help create a comfortable writing area.

Control Line Wrapping

Long lines can automatically wrap inside the Text widget.

For example:

text_area = tk.Text(

    window,

    wrap="word"

)

Common values include:

Value Behavior
word Wraps at word boundaries.
char Wraps at character boundaries.
none Disables automatic wrapping.

For normal writing applications, wrap="word" is often a good starting point.

Disable Editing

You can make the Text widget read-only by changing its state.

text_area.config(state="disabled")

To make it editable again:

text_area.config(state="normal")

This is useful when the widget is being used to display information rather than accept input.

Work with Selected Text

The Text widget supports text selection.

You can check whether text is selected by working with the sel tag.

For example:

try:

    selected = text_area.get("sel.first", "sel.last")

    print(selected)

except tk.TclError:

    print("No text selected")

The exception occurs when there is no active selection.

Delete Selected Text

You can remove selected text with:

text_area.delete(

    "sel.first",

    "sel.last"

)

Again, this should normally be done only when a selection exists.

Understand Text Indexes

Text widget indexes are an important concept in Tkinter.

A common index is:

"1.0"

This means the beginning of line 1.

Another example:

"2.5"

This means line 2, character position 5.

Tkinter also provides special indexes such as:

  • tk.END — the end of the text.
  • tk.INSERT — the current insertion cursor position.
  • tk.CURRENT — the position nearest the mouse pointer in relevant event contexts.
  • sel.first — beginning of the current selection.
  • sel.last — end of the current selection.
Beginner tip: You do not need to memorize every Text index immediately. Start with "1.0", tk.END, and selection indexes. More advanced text operations can be learned later.

Add a Scrollbar

A large Text widget should normally have a way to scroll through its content. Tkinter provides the Scrollbar widget for this.

Here is a basic vertical scrollbar example:

import tkinter as tk
window = tk.Tk()

window.title("Scrollable Text")

window.geometry("700x450")



frame = tk.Frame(window)

frame.pack(

    fill="both",

    expand=True,

    padx=20,

    pady=20

)


text_area = tk.Text(frame)

scrollbar = tk.Scrollbar(

    frame,

    command=text_area.yview

)

text_area.configure(

    yscrollcommand=scrollbar.set

)
text_area.pack(

    side="left",

    fill="both",

    expand=True

)

scrollbar.pack(

    side="right",

    fill="y"

)

window.mainloop()

There are two important connections here:

command=text_area.yview

and:

yscrollcommand=scrollbar.set

The first allows the scrollbar to control the Text widget. The second allows the Text widget to update the scrollbar position.

Build a Simple Text Editor

Now let's combine the concepts we have learned into a small text editor.

This application will include:

  • A multi-line Text widget
  • A vertical scrollbar
  • New button
  • Clear button
  • Show Content button
import tkinter as tk

from tkinter import messagebox


def new_document():

    text_area.delete("1.0", tk.END)


def clear_document():

    text_area.delete("1.0", tk.END)


def show_content():

    content = text_area.get("1.0", tk.END).strip()


    if content:

        messagebox.showinfo(

            "Content",

            content

        )

    else:

        messagebox.showwarning(

            "Empty",

            "There is no text to display."

        )


window = tk.Tk()



window.title("Simple Text Editor")

window.geometry("750x500")


toolbar = tk.Frame(window)

toolbar.pack(

    fill="x",

    padx=10,

    pady=10

)


new_button = tk.Button(

    toolbar,

    text="New",

    command=new_document

)


new_button.pack(

    side="left",

    padx=5

)


clear_button = tk.Button(

    toolbar,

    text="Clear",

    command=clear_document

)


clear_button.pack(

    side="left",

    padx=5

)


show_button = tk.Button(

    toolbar,

    text="Show Content",

    command=show_content

)

show_button.pack(

    side="left",

    padx=5

)


editor_frame = tk.Frame(window)



editor_frame.pack(

    fill="both",

    expand=True,

    padx=10,

    pady=10

)


text_area = tk.Text(

    editor_frame,

    wrap="word",

    font=("Consolas", 12)

)

scrollbar = tk.Scrollbar(

    editor_frame,

    command=text_area.yview

)



text_area.configure(

    yscrollcommand=scrollbar.set

)


text_area.pack(

    side="left",

    fill="both",

    expand=True

)

scrollbar.pack(

    side="right",

    fill="y"

)

text_area.insert(

    "1.0",

    "Welcome to the Tkinter Text Editor.\n\n"

    "Start writing here..."

)


window.mainloop()
A Simple Text Editor using Python Screenshot

This is still a beginner-level application, but it introduces the same building blocks used in larger desktop applications.

Read Text from the Editor

The important line is:

content = text_area.get("1.0", tk.END).strip()

This retrieves the entire document.

Once you have the content in a Python variable, you can later save it to a text file, search through it, process it, or send it to another part of your application.

Insert Text at the Cursor

You can insert text at the current cursor position using tk.INSERT.

text_area.insert(

    tk.INSERT,

    "Hello"

)

This is useful when building editor features such as inserting templates, symbols, or automatically generated text.

Get the Current Cursor Position

Tkinter can also provide information about the insertion cursor.

position = text_area.index(tk.INSERT)


print(position)

The returned value represents the current line and character position.

Common Beginner Mistakes

1. Using Entry Methods on Text

Entry and Text are different widgets.

For Entry:

entry.get()

For Text:

text_area.get("1.0", tk.END)

2. Forgetting the Text Index

This is incomplete:

text_area.get()

Text requires start and end indexes:

text_area.get("1.0", tk.END)

3. Forgetting to Clear the Correct Range

To clear everything:

text_area.delete("1.0", tk.END)

4. Creating a Scrollbar Without Connecting It

Creating only this:

scrollbar = tk.Scrollbar(frame)

does not automatically connect it to the Text widget.

You need both directions:

scrollbar = tk.Scrollbar(

    frame,

    command=text_area.yview

)



text_area.configure(

    yscrollcommand=scrollbar.set

)

5. Confusing Multi-Line and Single-Line Input

Use Entry when you primarily need a single line of input. Use Text when users need to enter or view multiple lines.

Practice Exercise

Create a simple Notes application.

Your application should contain:

  • A large Text widget.
  • A scrollbar.
  • A Clear button.
  • A Show Notes button.
  • A title at the top.

When the user clicks Show Notes, display the text they entered in a message box.

Try creating the application yourself before checking the solution.

Practice Solution

import tkinter as tk

from tkinter import messagebox





def clear_notes():

    notes.delete("1.0", tk.END)





def show_notes():

    content = notes.get("1.0", tk.END).strip()



    if content:

        messagebox.showinfo(

            "My Notes",

            content

        )

    else:

        messagebox.showwarning(

            "Notes",

            "No notes entered."

        )





window = tk.Tk()



window.title("Notes App")

window.geometry("650x450")


title = tk.Label(

    window,

    text="My Notes",

    font=("Arial", 22, "bold")

)



title.pack(pady=15)



editor_frame = tk.Frame(window)



editor_frame.pack(

    fill="both",

    expand=True,

    padx=15,

    pady=10

)





notes = tk.Text(

    editor_frame,

    wrap="word",

    font=("Arial", 13)

)



scrollbar = tk.Scrollbar(

    editor_frame,

    command=notes.yview

)



notes.configure(

    yscrollcommand=scrollbar.set

)


notes.pack(

    side="left",

    fill="both",

    expand=True

)



scrollbar.pack(

    side="right",

    fill="y"

)


button_frame = tk.Frame(window)



button_frame.pack(pady=10)


show_button = tk.Button(

    button_frame,

    text="Show Notes",

    command=show_notes,

    width=15

)


show_button.pack(

    side="left",

    padx=5

)


clear_button = tk.Button(

    button_frame,

    text="Clear",

    command=clear_notes,

    width=15

)



clear_button.pack(

    side="left",

    padx=5

)


window.mainloop()

Frequently Asked Questions

What is the Tkinter Text widget?

The Text widget is used to display and accept multi-line text in a Tkinter GUI application.

How do I get all text from a Text widget?

Use:

text_area.get("1.0", tk.END)

How do I clear a Tkinter Text widget?

Use:

text_area.delete("1.0", tk.END)

What does "1.0" mean in Tkinter Text?

It represents the beginning of line 1: line number 1 and character position 0.

How do I add a scrollbar to a Text widget?

Connect the scrollbar's command to the Text widget's yview and the Text widget's yscrollcommand to the scrollbar's set method.

What is the difference between Tkinter Entry and Text?

Entry is primarily intended for single-line input, while Text supports multi-line text.

Can Tkinter Text be used to create a text editor?

Yes. The Text widget can serve as the main editing area of a basic text editor. Features such as file opening, saving, search, menus, and keyboard shortcuts can be added later.

Conclusion

The Tkinter Text widget is an important component for applications that need multi-line text input or display. It provides methods for inserting, reading, deleting, selecting, and managing text.

In this tutorial, you learned how to:

  • Create a Tkinter Text widget
  • Set its size
  • Insert text
  • Read text using get()
  • Delete and clear text
  • Customize fonts and colors
  • Control line wrapping
  • Disable editing
  • Work with selected text
  • Understand Text indexes
  • Add a scrollbar
  • Build a simple text editor

You now have three important Tkinter widgets in your toolkit: Label, Button, and Entry/Text. These widgets are enough to start building many small interactive GUI applications.

Previous:
Tkinter Tutorial for Beginners: Introduction to Python GUI Development

Previous Post Next Post