Home » Tutorials » How to Make a Simple Spelling Checker using Python

How to Make a Simple Spelling Checker using Python

In today’s article, we are going to set up a code that is going to spell check every text or word the user is going to enter by simply clicking the button.

Let’s get started!

Table of Contents

Necessary Libraries

For this code to function properly, make sure to install the tkinter and textblob libraries via your terminal or your command prompt:

$ pip install tk
$ pip install textblob

Imports

from tkinter import *
from textblob import Word

First, we start by importing the tkinter library to help us create a graphical user interface (GUI), and from textblob we import Word class which is going to be used for spell checking.

Check_spelling Function

def check_spelling():
    user_input = entry.get()
    word = Word(user_input)
    corrected_word = word.correct()

    result_label.config(text=f"Corrected Text: {corrected_word}")

Next, we define a function that gets triggered once the “check spelling” button is clicked, it retrieves the user’s input and stores it in the variable user_input, and then the corrected version of the word is called using the correct() method which is then displayed using the config() method.

Setting up The Main Window

# Create a main window
root = Tk()
root.title("Simple Spell Checker - The Pycodes")
root.geometry("500x200")
root.configure(bg="#f0e68c")

Here, we created the main window and gave it a title, dimensions, and background color.

GUI Elements

# Create and configure a label
label = Label(root, text="Enter a word or phrase:" ,font=18, bg="#f0e68c")
label.pack(pady=10)


# Create an entry widget for user input
entry = Entry(root, width=40,font=14)
entry.pack()


# Create a button to check spelling
check_button = Button(root, text="Check Spelling",bg="#8b4513",font=18, command=check_spelling)
check_button.pack(pady=10)


# Create a label to display the result
result_label = Label(root, text="", wraplength=300,font=14,bg="#f0e68c")
result_label.pack()

Following that, we create a label that asks the user can write his input followed by an entry widget where he can do that, then we create a button once clicked executes the check_spelling function.

After that, we create a label initially empty then once the button is clicked the correct word appears there.

Tkinter Mainloop

# Start the Tkinter main loop
root.mainloop()

Finally, this part allows the main window to run continuously until the user exits the main window.

Example

Full Code

from tkinter import *
from textblob import Word


def check_spelling():
    user_input = entry.get()
    word = Word(user_input)
    corrected_word = word.correct()


    result_label.config(text=f"Corrected Text: {corrected_word}")


# Create a main window
root = Tk()
root.title("Simple Spell Checker - The Pycodes")
root.geometry("500x200")
root.configure(bg="#f0e68c")


# Create and configure a label
label = Label(root, text="Enter a word or phrase:" ,font=18, bg="#f0e68c")
label.pack(pady=10)


# Create an entry widget for user input
entry = Entry(root, width=40,font=14)
entry.pack()


# Create a button to check spelling
check_button = Button(root, text="Check Spelling",bg="#8b4513",font=18, command=check_spelling)
check_button.pack(pady=10)


# Create a label to display the result
result_label = Label(root, text="", wraplength=300,font=14,bg="#f0e68c")
result_label.pack()


# Start the Tkinter main loop
root.mainloop()

Happy Coding!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top