Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Make Notepad using Tkinter
Next article icon

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
fromtkinterimport*fromtextblobimportTextBlob# Function to clear both the text entry boxesdefclearAll():# whole content of text entry area is deletedword1_field.delete(0,END)word2_field.delete(0,END)# Function to get a corrected worddefcorrection():# get a content from entry boxinput_word=word1_field.get()# create a TextBlob objectblob_obj=TextBlob(input_word)# get a corrected wordcorrected_word=str(blob_obj.correct())# insert method inserting the# value in the text entry box.word2_field.insert(10,corrected_word)# Driver codeif__name__=="__main__":# Create a GUI windowroot=Tk()# Set the background colour of GUI windowroot.configure(background='light green')# Set the configuration of GUI window (WidthxHeight)root.geometry("400x150")# set the name of tkinter GUI windowroot.title("Spell Corrector")# Create Welcome to Spell Corrector Application: labelheadlabel=Label(root,text='Welcome to Spell Corrector Application',fg='black',bg="red")# Create a "Input Word": labellabel1=Label(root,text="Input Word",fg='black',bg='dark green')# Create a "Corrected Word": labellabel2=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 functionbutton1=Button(root,text="Correction",bg="red",fg="black",command=correction)button1.grid(row=2,column=1)# Create a Clear Button and attached# with clearAll functionbutton2=Button(root,text="Clear",bg="red",fg="black",command=clearAll)button2.grid(row=4,column=1)# Start the GUIroot.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 400x150 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.

Spelling Checker Program Using Python Tkinter | Python Project

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp