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.