Spell Check With GUI


Not Complete
Views: 25
Published Date: Jan. 7, 2019, 4 p.m.
Section: Strings

Join Our Mailing List

Get the latest tutorials right to your inbox and you can unsubscribe anytime.


Spell Check With GUI

Welcome to another Python Challenge, today will challenge you to build a graphical user interface for our spell check program. 

The Challenge

We ask that you take our previous challenge and build a GUI for that program. If you haven't completed our spell check challenge we suggest you complete it first and that challenge can be located here https://www.mastercode.online/courses/tutorial/spell-check/

The goal here is to build a GUI that takes the users string and returns suggestions for misspelled words. Give it a try and let us know how it works out for you.

The Solution

We hope you were able to build a cool GUI. We started out this challenge writing a GUI from scratch but we stumbled onto a pretty cool tool that we will use to build a GUI. This tool is called PySimpleGUI and the documentation can be found at https://pysimplegui.readthedocs.io/.

Let's get started!

1. We will first need to install the PySimpleGUI.

pip3 install PySimpleGUI

2. We will now import the package

import PySimpleGUI as sg

3. We will also need to import difflib.

import PySimpleGUI as sg
import difflib

4. Now insert our database of words. Not really a database but just a list.

import PySimpleGUI as sg
import difflib

words = ['i', 'like', 'dogs', 'dog', 'hockey']

5. Now input our word_check function this will change slightly instead of printing to the terminal we will return a value to be used in the GUI.

import PySimpleGUI as sg
import difflib

words = ['i', 'like', 'dogs', 'dog', 'hockey']

def word_check(s):
    for word in s.casefold().split():
        if word not in words:
            suggestion = difflib.get_close_matches(word, words)
            return f"Did you mean {', '.join(str(x) for x in suggestion)} instead of {word}?"

6. Now let's build our layout for our application. Using PySimpleGUI this process is so simple compared to Tkinter. We will create a variable named layout. This will hold a list of widgets. Each index position inside our list makes up a row (i.e. horizontal). Inside each row, we input which widgets we want. We then can modify the widgets. Check out our example below. This is so simple.

import PySimpleGUI as sg
import difflib

words = ['i', 'like', 'dogs', 'dog', 'hockey']

def word_check(s):
    for word in s.casefold().split():
        if word not in words:
            suggestion = difflib.get_close_matches(word, words)
            return f"Did you mean {', '.join(str(x) for x in suggestion)} instead of {word}?"


layout = [[sg.Text("Spell Checker", justification='center', font=("Helvetica", 25))],
        [sg.Multiline(do_not_clear=True, key='_IN_')],
        [sg.Text("", size=(50, 10), key=('_OUT_'))],
        [sg.Button("Spell Check"), sg.Button("Exit")]]

7. Now we will initialize our GUI and provide a title for the window.

import PySimpleGUI as sg
import difflib

words = ['i', 'like', 'dogs', 'dog', 'hockey']

def word_check(s):
    for word in s.casefold().split():
        if word not in words:
            suggestion = difflib.get_close_matches(word, words)
            return f"Did you mean {', '.join(str(x) for x in suggestion)} instead of {word}?"


layout = [[sg.Text("Spell Checker", justification='center', font=("Helvetica", 25))],
        [sg.Multiline(do_not_clear=True, key='_IN_')],
        [sg.Text("", size=(50, 10), key=('_OUT_'))],
        [sg.Button("Spell Check"), sg.Button("Exit")]]

window = sg.Window('Spell Checker').Layout(layout)

8. We will now build the logic. We will set up a loop that will allow our application to run, we will add the ability to kill the application and we will add a logic that will take the data input into the Multiline widget and run it through our word_check function. If there are any misspelled words we will return them to the user. Check out the simplicity of this code.

The key to the functionality is the last line below. We find the element with the key _OUT_ this will allow us to get the data from the users then we run that data through our word_check function and return the data back to the element with the key _IN_. 

import PySimpleGUI as sg
import difflib

words = ['i', 'like', 'dogs', 'dog', 'hockey']

def word_check(s):
    for word in s.casefold().split():
        if word not in words:
            suggestion = difflib.get_close_matches(word, words)
            return f"Did you mean {', '.join(str(x) for x in suggestion)} instead of {word}?"


layout = [[sg.Text("Spell Checker", justification='center', font=("Helvetica", 25))],
        [sg.Multiline(do_not_clear=True, key='_IN_')],
        [sg.Text("", size=(50, 10), key=('_OUT_'))],
        [sg.Button("Spell Check"), sg.Button("Exit")]]

window = sg.Window('Spell Checker').Layout(layout)

while True:
    event, values = window.Read()

    if event is None or event == 'Exit':
        break

    if event == "Spell Check":
        window.FindElement('_OUT_').Update(word_check(values['_IN_']))

9. When we break out of the while loop we need to call window.Close() to close the program. That is all we need to make a fully functional GUI for our spell_checker.

import PySimpleGUI as sg
import difflib

words = ['i', 'like', 'dogs', 'dog', 'hockey']

def word_check(s):
    for word in s.casefold().split():
        if word not in words:
            suggestion = difflib.get_close_matches(word, words)
            return f"Did you mean {', '.join(str(x) for x in suggestion)} instead of {word}?"


layout = [[sg.Text("Spell Checker", justification='center', font=("Helvetica", 25))],
        [sg.Multiline(do_not_clear=True, key='_IN_')],
        [sg.Text("", size=(50, 10), key=('_OUT_'))],
        [sg.Button("Spell Check"), sg.Button("Exit")]]

window = sg.Window('Spell Checker').Layout(layout)

while True:
    event, values = window.Read()

    if event is None or event == 'Exit':
        break

    if event == "Spell Check":
        window.FindElement('_OUT_').Update(word_check(values['_IN_']))


window.Close()

Conclusion

In this challenge, you were challenged to build a simple GUI for our spell check program. With the introduction of the PySimpleGUI we are now able to build GUI's in no time flat. We will continue to add GUI challenges to help everyone learn how to build full fledge applications.



Ask A Question


Similar Tutorials


Spell Check With GUI

Find A Substring

Spell Check

Sort String In Alphabetical Order And Remove Duplicates

Users Name Return A Message

Last 5 Tutorials


Spell Check With GUI

Spell Check

Math Game

Find A Substring

Sort String In Alphabetical Order And Remove Duplicates

Most Popular


Introduction To Command Line and Python Commands

Install Python 3.7 On Mac OS X

Upload A CSV File To Django

Introduction To Python 3.7 Tutorial Series

How To Use The Django For Loop Counter