Skip to content

Commit 1c59af2

Browse files
committedFeb 9, 2023
add realtime spelling checker tutorial
1 parent a25e9d2 commit 1c59af2

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-0
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -260,5 +260,6 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
260260
- [How to Make a Chess Game with Pygame in Python](https://www.thepythoncode.com/article/make-a-chess-game-using-pygame-in-python). ([code](gui-programming/chess-game))
261261
- [How to Build a GUI QR Code Generator and Detector Using Python](https://www.thepythoncode.com/article/make-a-qr-code-generator-and-reader-tkinter-python). ([code](gui-programming/qrcode-generator-reader-gui))
262262
- [How to Build a GUI Dictionary App with Tkinter in Python](https://www.thepythoncode.com/article/make-a-gui-audio-dictionary-python). ([code](gui-programming/word-dictionary-with-audio))
263+
- [How to Make a Real-Time GUI Spelling Checker in Python](https://www.thepythoncode.com/article/make-a-realtime-spelling-checker-gui-python). ([code](gui-programming/realtime-spelling-checker))
263264

264265
For any feedback, please consider pulling requests.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Make a Real-Time GUI Spelling Checker in Python](https://www.thepythoncode.com/article/make-a-realtime-spelling-checker-gui-python)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nltk
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# this imports everything from the tkinter module
2+
from tkinter import *
3+
# importing the ttk module from tkinter that's for styling widgets
4+
from tkinter import ttk
5+
# importing a Text field with the scrollbar
6+
from tkinter.scrolledtext import ScrolledText
7+
# imports the re module
8+
import re
9+
# this imports nltk
10+
import nltk
11+
# importing all the words from nltk
12+
from nltk.corpus import words
13+
# importing an askyesno message box from tkinter.message
14+
from tkinter.messagebox import askyesno
15+
16+
# this will download the words
17+
nltk.download('words')
18+
19+
# we are creating a SpellingChecker class
20+
class SpellingChecker:
21+
# a special method, always called when an instance is created
22+
def __init__(self, master):
23+
# defining a style for the label
24+
style = ttk.Style()
25+
# configuring the style, TLabel is the style name
26+
style.configure('TLabel', foreground='#000000', font=('OCR A Extended', 25))
27+
# variable for tracking white space, default is 0
28+
self.old_spaces = 0
29+
# creating the main window
30+
self.master = master
31+
# giving title to the main window
32+
self.master.title('Real-Time Spelling Checker')
33+
# defining dimensions and position for the main window
34+
self.master.geometry('580x500+440+180')
35+
# adding an icon to the main window
36+
self.master.iconbitmap(self.master, 'spell-check.ico')
37+
# making the main window non-resizable
38+
self.master.resizable(height=FALSE, width=FALSE)
39+
# this is for closing the window via the close() function
40+
self.master.protocol('WM_DELETE_WINDOW', self.close)
41+
# creating the label to display the big text
42+
self.label = ttk.Label(self.master, text='Real-Time Spelling Checker', style='TLabel')
43+
# adding the label to the main window using grid geometry manager
44+
self.label.grid(row=0, column=0, columnspan=10, padx=5, pady=25)
45+
# creating a scollable Text field
46+
self.text = ScrolledText(self.master, font=("Helvetica", 15), width=50, height=15)
47+
# bing the scrollable Text field to an event
48+
self.text.bind('<KeyRelease>', self.check)
49+
# adding the scrollable Text field to the main window using grid geometry manager
50+
self.text.grid(row=1, column=0, padx=5, pady=5, columnspan=10)
51+
52+
# the function for closing the application
53+
def close(self):
54+
# this will ask the user whether to close or not
55+
# if the value is yes/True the window will close
56+
if askyesno(title='Close Real-Time Spelling Checker', message='Are you sure you want to close the application?'):
57+
# this destroys the window
58+
self.master.destroy()
59+
60+
# this is the function for checking spelling in real-time
61+
def check(self, event):
62+
# getting all the content from the ScrolledText via get() function
63+
# 1.0 is the starting point and END is the end point of the ScrolledText content
64+
content = self.text.get('1.0', END)
65+
# getting all the white spaces from the content
66+
space_count = content.count(' ')
67+
# checking if the space_count is not equal to self.old_spaces
68+
if space_count != self.old_spaces:
69+
# updating the self.old_spaces to space_count
70+
self.old_spaces = space_count
71+
# this loops through all the tag names
72+
# and deletes them if the word is valid
73+
for tag in self.text.tag_names():
74+
self.text.tag_delete(tag)
75+
# splitting the content by white space
76+
# and looping through the split content to get a single word
77+
for word in content.split(' '):
78+
# with the sub() function we are removing special characters from the word
79+
# replacing the special character with nothing
80+
# the target is word.lower()
81+
# checking if the cleaned lower case word is not in words
82+
if re.sub(r'[^\w]', '', word.lower()) not in words.words():
83+
# gets the position of the invalid word
84+
position = content.find(word)
85+
# adding a tag to the invalid word
86+
self.text.tag_add(word, f'1.{position}', f'1.{position + len(word)}')
87+
# changing the color of the invalid word to red
88+
self.text.tag_config(word, foreground='red')
89+
90+
91+
92+
# creating the root winding using Tk() class
93+
root = Tk()
94+
# instantiating/creating object app for class SpellingChecker
95+
app = SpellingChecker(root)
96+
# calling the mainloop to run the app infinitely until user closes it
97+
root.mainloop()

0 commit comments

Comments
 (0)
Please sign in to comment.