How to get the current length of the Text in a Tkinter Text widget?

The Text widget in Tkinter supports multiline user input from the user. We can configure the Text widget properties such as its font properties, text color, background, etc., by using the configure() method.

To create an application that will count the currently written characters in a Text widget, we can follow these steps ?

  • Create a Text widget and define its width and height properties.

  • A label widget is needed to display the total count of the characters.

  • Define an event with <KeyPress> and <KeyRelease> functionality that will show the updated character count in the label widget.

  • The function will have a label configuration that gets updated whenever the event takes place. To display the character count, specify the value of the text by casting the length of the characters.

  • Pack the widgets and display the output.

Using get() Method with Index Range

The most reliable way to get the current text length is using the get("1.0", 'end-1c') method. The index "1.0" represents the beginning, and 'end-1c' represents the end minus one character (to exclude the trailing newline) ?

# Import the required libraries
from tkinter import *

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

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

# Define a function to get the length of the current text
def update(event):
    current_length = len(text.get("1.0", 'end-1c'))
    label.config(text="Total Characters: " + str(current_length))

# Create a text widget
text = Text(win, width=50, height=10, font=('Calibri', 14))
text.pack()

# Create a Label widget
label = Label(win, text="Total Characters: 0", justify=CENTER, font=('Arial', 11))
label.pack()

# Bind the events for real-time updates
text.bind('<KeyPress>', update)
text.bind('<KeyRelease>', update)

win.mainloop()

Alternative Method Using StringVar

You can also track text length using a StringVar, though this approach is less common for Text widgets ?

from tkinter import *

def update_count(*args):
    count = len(text.get("1.0", 'end-1c'))
    count_label.config(text=f"Character Count: {count}")

win = Tk()
win.geometry("600x300")

text = Text(win, width=40, height=8)
text.pack(pady=10)

count_label = Label(win, text="Character Count: 0", font=('Arial', 12))
count_label.pack()

# Update on any key event
text.bind('<KeyRelease>', lambda e: update_count())

win.mainloop()

Key Points

  • Index Range: Use "1.0" for start and 'end-1c' for end to avoid counting the trailing newline

  • Event Binding: Bind both <KeyPress> and <KeyRelease> for immediate updates

  • Real-time Updates: The character count updates as you type or delete text

Output

Running the above code will display a text editor and a label widget at the bottom. Whenever we type something in the text editor, it will get updated with the "Total Character:" count.

This is sample text in the Text widget. You can type multiple lines here. Total Characters: 67 Tkinter Text Length Counter

Conclusion

Use the get("1.0", 'end-1c') method to retrieve text content and len() to count characters. Bind both KeyPress and KeyRelease events for real-time character count updates in your Tkinter Text widget.

Updated on: 2026-03-26T00:14:23+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements