We are creating a simple ‘Word Counter’ with the help of Python Tkinter. Python Tkinter is a library of Python by importing we can make the graphical user interface (GUI). Python Tkinter (GUI) is the fastest and easiest way to create GUI. In this tutorial, we’ll walk you through the process of building a “Word Counter” tool using Python and Tkinter. This project is perfect for beginners who want to explore GUI programming and developers looking to practice and brush up on their Python skills.
The “Word Counter” tool will help us count the word.
For individuals dealing with text, such as writers and students, utilizing a word count tool is very helpful. It guarantees that content complies with specified length guidelines, improving overall clarity. Writers count on it to assess document length for streamlined editing, while students adhere to word constraints for academic tasks. Within professional environments, it contributes to developing impactful advertising materials and reports. Moreover, in the realms of SEO and digital marketing, maintaining suitable word counts can increase search engine rankings and reader involvement. Ultimately, a word count tool enhances productivity, accuracy, and excellence in writing, proving itself to be a very vital resource.
Step-by-Step: Build Your Own Tkinter ‘Word Counter’
Our plan to create the tool:
At first, we import tkinter by using ‘ from tkinter import * ‘ .After that, we call functions for everything like undo, clear, and submit. Also, create a button for all these functions by using the button after all things by calling the function we execute the code.
Steps for creating the game and code Explanations:
Here we have written the code using Python language.
Importing Modules:
from tkinter import * # for improting the tkinter Library import re
- Imports Tkinter library for creating the GUI.
- Imports ‘re’ for regular expressions to help count words.
Creating Main Window:
window = Tk() window.title("WORD_COUNTER") # This is for creating the title window.geometry("810x810") # This is the dimension
- Creates the main window titled “WORD_COUNTER” with dimensions 810×810 pixels.
Initializing Variables:
no_of_words = 0 # This is for the word no_of_char = 0 # This is for the character
- Initializes variables to store the number of words and characters that have the initial value of zero.
Function Definitions:
- Submit Function:
def submit(): global no_of_words lab5.config(text="There are " + str(no_of_words) + " words")
- Updates a label to show the current word count on the display.
- Clear Function:
def clear(): global no_of_words text1.delete(1.0, END) no_of_words = 0 lab3.config(text=str(no_of_words)) lab5.config(text="")
- It’s the uses for the clearing written word.
- Undo Function:
def undo(): global no_of_words global no_of_char lab5.config(text="") if int(no_of_char) > 0: data = text1.get(1.0, "end-2c") text1.delete(1.0, END) text1.insert(1.0, data) count = int(no_of_char - 1) if count > 0: words = re.findall(r"[\S]+", text1.get(1.0, END)) no_of_words = len(words) chars = len(text1.get(1.0, END)) no_of_char = str(chars - 1) lab3.config(text=str(no_of_words)) elif count == 0: no_of_char = 0 no_of_words = 0 lab3.config(text=str(no_of_words))
- This undo function removes the last word from the written text.
- Key Release Function:
def key_release(b): global no_of_words global no_of_char lab5.config(text="") words = re.findall(r"[\S]+", text1.get(1.0, END)) no_of_words = len(words) chars = len(text1.get(1.0, END)) no_of_char = str(chars - 1) lab3.config(text=str(no_of_words))
- Updates word counts each time a key is released.
Creat Labels:
- Total Words Label:
lab1 = Label(window, text="Total Words", font=("Arial", 15)) lab1.place(x=5, y=10)
- Label for total Words.
- Word Count Display Label:
lab3 = Label(window, text="0", font=("Arial", 15)) lab3.place(x=150, y=10)
- This label displays the word count.
- Message Display Label:
lab5 = Label(window, text="", font=("Arial", 25), fg="blue") lab5.place(x=400, y=600)
- Label to display messages, like the word count after clicking the “Submit” button.
Creating Text Entry Box:
text1 = Text(window, width=110, relief=RAISED, height=20, takefocus=0, font=("Arial", 15), fg="grey", wrap=WORD, spacing1=1) text1.place(x=5, y=50)
- This is for creating the text box.
Creating Button:
- Submit Button:
btn1 = Button(window, text="SUBMIT", font=("Arial", 15), command=submit, fg="grey") btn1.place(x=500, y=550)
- This button is for submitting to display the final result.
- Clear Button:
btn2 = Button(window, text="CLEAR", font=("Arial", 15), command=clear, fg="grey") btn2.place(x=720, y=550)
- This button is for clearing the entire box text.
- Undo Button:
btn3 = Button(window, text="UNDO", font=("Arial", 15), command=undo, fg="grey") btn3.place(x=900, y=550)
- This is for the undo button to remove the last char by using this button.
Binding Key Release:
text1.bind("<KeyRelease>", lambda a: key_release(a))
- Binds the key release event to the key_release function, so the word count updates every time a key is released.
Running the Application:
window.mainloop()
- ‘mainloop()’ runs the Tkinter event loop, making the window responsive to user inputs.
The Entire Code is:
from tkinter import * import re window = Tk() window.title("WORD_COUNTER") window.geometry("810x810") #window["bg"] = "green" no_of_words = 0 no_of_char=0 # Function Calling def submit(): global no_of_words lab5.config(text="There are "+ str(no_of_words)+" words") def clear(): global no_of_words text1.delete(1.0,END) no_of_words = 0 lab3.config(text=str(no_of_words)) lab5.config(text="") def undo(): global no_of_words global no_of_char lab5.config(text="") if int(no_of_char)>0: data = text1.get(1.0,"end-2c") text1.delete(1.0,END) text1.insert(1.0,data) count = int(no_of_char-1) if count>0: words = re.findall(r"[\S]+",text1.get(1.0,END)) no_of_words = len(words) chars = len(text1.get(1.0,END)) no_of_char = str(chars-1) lab3.config(text=str(no_of_words)) elif count == 0: no_of_char = 0 no_of_words = 0 lab3.config(text=str(no_of_words)) def key_release(b): global no_of_words global no_of_char lab5.config(text="") words = re.findall(r"[\S]+",text1.get(1.0,END)) no_of_words = len(words) chars = len(text1.get(1.0,END)) no_of_char = str(chars-1) lab3.config(text=str(no_of_words)) lab1 = Label( window, text="Total Words", font=("Arial",15) ) lab1.place(x=5,y=10) lab3 = Label( window, text="0", font=("Arial",15) ) lab3.place(x=150,y=10) # This label is for ans display purpouse lab5 = Label( window, text="", font=("Arial",25), fg="blue" ) lab5.place(x=400,y=600) text1 = Text( window, width=110, relief=RAISED, height=20, takefocus= 0 , font=("Arial",15), fg="grey", wrap=WORD, spacing1=1 ) text1.place(x=5,y=50) # Making Three button "SUBMIT" btn1 = Button( window, text="SUBMIT", font=("Arial",15), command=submit, fg="grey" ) btn1.place(x=500,y=550) # This button is for "CLEAR" btn2 = Button( window, text="CLEAR", font=("Arial",15), command=clear, fg="grey" ) btn2.place(x=720,y=550) # This is for "UNDO" btn3 = Button( window, text="UNDO", font=("Arial",15), command=undo, fg="grey" ) btn3.place(x=900,y=550) text1.bind("<KeyRelease>", lambda a:key_release(a)) window.mainloop()
Output:
This is the GIF link: GIF