- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 and 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.
Example
# 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): label.config(text="Total Characters: "+str(len(text.get("1.0", 'end-1c')))) # Create a text widget text=Text(win, width=50, height=10, font=('Calibri 14')) text.pack() # Create a Label widget label=Label(win, text="", justify=CENTER, font=('11')) label.pack() # Bind the buttons with the event text.bind('<KeyPress>', update) text.bind('<KeyRelease>', update) win.mainloop()
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.
- Related Articles
- How to highlight the current line of a Text widget in Tkinter?
- Get the text of a button widget in Tkinter
- How to get the input from the Tkinter Text Widget?
- How to highlight text in a tkinter Text widget?
- How do I center the text in a Tkinter Text widget?
- How to clear the contents of a Tkinter Text widget?
- How to take input in a text widget and display the text in tkinter?
- How to make the Tkinter text widget read only?
- How to erase everything from the Tkinter text widget?
- How to create hyperlink in a Tkinter Text widget?
- How to change the color of certain words in a Tkinter text widget?
- How to erase everything from a Tkinter text widget?
- How to attach a vertical scrollbar in Tkinter text widget?
- Printing a list to a Tkinter Text widget
- How to attach a Scrollbar to a Text widget in Tkinter?
