Open In App

Python – Spell Corrector GUI using Tkinter

Improve
Improve
Like Article
Like
Save
Share
Report

Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. Python with Tkinter outputs the fastest and easiest way to create GUI applications. In this article, we will learn how to create a GUI Spell Corrector application using Tkinter, with a step-by-step guide.

Prerequisites: Introduction to Tkinter | spell checking 

To create a Tkinter:

  • Importing the module – tkinter
  • Create the main window (container)
  • Add any number of widgets to the main window.
  • Apply the event Trigger on the widgets.

The GUI would look like below:

Let’s create a GUI-based Spell Corrector application that can correct the word given by the user.

Below is the implementation: 

Python3




from tkinter import *
from textblob import TextBlob
 
# Function to clear both the text entry boxes
 
 
def clearAll():
 
    # whole content of text entry area is deleted
    word1_field.delete(0, END)
    word2_field.delete(0, END)
 
# Function to get a corrected word
 
 
def correction():
 
    # get a content from entry box
    input_word = word1_field.get()
 
    # create a TextBlob object
    blob_obj = TextBlob(input_word)
 
    # get a corrected word
    corrected_word = str(blob_obj.correct())
 
    # insert method inserting the
    # value in the text entry box.
    word2_field.insert(10, corrected_word)
 
 
# Driver code
if __name__ == "__main__":
 
    # Create a GUI window
    root = Tk()
 
    # Set the background colour of GUI window
    root.configure(background='light green')
 
    # Set the configuration of GUI window (WidthxHeight)
    root.geometry("400x150")
 
    # set the name of tkinter GUI window
    root.title("Spell Corrector")
 
    # Create Welcome to Spell Corrector Application: label
    headlabel = Label(root, text='Welcome to Spell Corrector Application',
                      fg='black', bg="red")
 
    # Create a "Input Word": label
    label1 = Label(root, text="Input Word",
                   fg='black', bg='dark green')
 
    # Create a "Corrected Word": label
    label2 = Label(root, text="Corrected Word",
                   fg='black', bg='dark green')
 
    # grid method is used for placing
    # the widgets at respective positions
    # in table like structure .
    # padx keyword argument used to set padding along x-axis .
    headlabel.grid(row=0, column=1)
    label1.grid(row=1, column=0)
    label2.grid(row=3, column=0, padx=10)
 
    # Create a text entry box
    # for filling or typing the information.
    word1_field = Entry()
    word2_field = Entry()
 
    # padx keyword argument used to set padding along x-axis .
    # pady keyword argument used to set padding along y-axis .
    word1_field.grid(row=1, column=1, padx=10, pady=10)
    word2_field.grid(row=3, column=1, padx=10, pady=10)
 
    # Create a Correction Button and attached
    # with correction function
    button1 = Button(root, text="Correction", bg="red", fg="black",
                     command=correction)
 
    button1.grid(row=2, column=1)
 
    # Create a Clear Button and attached
    # with clearAll function
    button2 = Button(root, text="Clear", bg="red",
                     fg="black", command=clearAll)
 
    button2.grid(row=4, column=1)
 
    # Start the GUI
    root.mainloop()


Output:

Code Explanation:

  1. The code starts by importing all the necessary functions and classes from the tkinter library.
  2. Next, the code creates a window object called root and sets its background color to light green.
  3. The window’s width and height are then set according to user preferences.
  4. Finally, the window’s title is set to “Spell Corrector”.
  5. Next, the clearAll() function is called.
  6. This function clears both the text entry boxes onscreen.
  7. The word1_field object is first deleted from 0 to END , and then word2_field is deleted from 0 to END.
  8. The correction() function is next called.
  9. This function gets a content from input_word , which was entered into one of the text entry boxes earlier using keyboard input.
  10. The corrected_word variable will be used later in this function to insert the correct word into word2_field .
  11. The insertion method for word2_field is then invoked using keyword argument 10.
  12. This argument specifies that corrected_word should be inserted at position 10 inside of word2_field .
  13. After inserting corrected_word , clearAll() is called once again so that any unsaved changes made in this code can be reflected onscreen.
  14. The code creates a window and sets the background color to light green.
  15. The width and height of the window are set to 400×150 pixels.
  16. Finally, the name of the window is set to “Spell Corrector”.
  17. Now that you have created the Spell Corrector GUI window, let’s take a look at how it works.
  18. First, clearAll() function deletes all content from both text entry boxes.
  19. Next, correction() function gets a content from one of the text entry boxes and creates a TextBlob object containing this content.
  20. Finally, word2_field.insert(10, corrected_word) is used to insert this corrected word into the other text entry box.
  21. The code starts by creating a Welcome to Spell Corrector Application label.
  22. This label will be the first thing that users see when they open the application.
  23. Next, the code creates two labels: an Input Word label and a Corrected Word label.
  24. The Input Word label will display the text “Input Word” and the Corrected Word label will display the text “Corrected Word.”
  25. The code then uses grid method to place these labels at respective positions in a table-like structure.
  26. The grid method is used so that each label is positioned evenly across the screen.
  27. The code creates a basic application with a header and two labels.
  28. The first label, labeled “Headlabel” will display the text “Welcome to Spell Corrector Application”.
  29. The second label, labeled “Label1” will display the input word.
  30. The third label, labeled “Label2” will display the corrected word.
  31. The code uses the grid method to place the widgets at respective positions in a table-like structure.


Last Updated : 17 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads