A dictionary is an essential resource when learning any language. With concise definitions, it makes learning and understanding a language much easier. With the invention of smartphones, you can access such an invaluable app in minutes.

Build a dictionary application using Python's Tkinter and PyMultiDictionary modules to get the meaning, synonyms, and antonyms of any word.

The Tkinter and PyMultiDictionary Modules

Tkinter is the standard Python GUI library that you can use to create desktop applications. It offers a variety of widgets like buttons, labels, and text boxes, so you can develop apps in no time. You can use it to create simple utilities like a spelling corrector, or games like this color recognition test.

To install Tkinter, open a terminal and run:

        pip install tkinter
    

You can use the PyMultiDictionary module to get meanings, translations, synonyms, and antonyms of words in 20 different languages. To install PyMultiDictionary on your system, run this command:

        pip install PyMultiDictionary
    

How to Build a Dictionary App Using Python

You can find the source code of the Dictionary App in this GitHub repository.

Begin by importing the Tkinter and the PyMultiDictionary modules. Instantiate the MultiDictionary class and initialize the root window. Set the title and dimensions of your application.

        from tkinter import *
from PyMultiDictionary import MultiDictionary

dictionary = MultiDictionary()
root = Tk()
root.title("Word Dictionary Using Python")
root.geometry("1250x750")

Define a function, dict(). This function will set the text of the meaning, synonym, and antonym labels to the result of each method call.

Pass the language ("en" for English) and the word the user entered to the meaning method. This method returns a tuple containing the word type, its dictionary definition, and its description from Wikipedia. Extract the second value from this tuple—the definition—and pass it to the Label.config() method.

Call the synonym and antonym methods, passing the same parameters. These methods each return a list that you can pass directly to config().

        def dict():
    meaning.config(text=dictionary.meaning('en', word.get())[1])
    synonym.config(text=dictionary.synonym('en', word.get()))
    antonym.config(text=dictionary.antonym('en', word.get()))

Define a label to display the name of the application. Set the window you want to place the label in, the text it should have, and the font styles along with the font color. Use the pack() method to organize the label by giving it a horizontal padding of 10.

Define a frame in the root window and a label to ask the user to enter a word. Pass the parameters as before and place the widget on the left side. Define an entry widget to give the user an area for word input. Add it to the frame widget and define its font styles as well. Organize and add some padding to both widgets.

        Label(root, text="Word Dictionary Using Python", font=("Arial 36 bold"),
 fg="Purple").pack(pady=10)

frame = Frame(root)
Label(frame, text="Type Word:", font=("Arial 28 bold")).pack(side=LEFT)
word = Entry(frame, font=("Arial 23 bold"))
word.pack()
frame.pack(pady=10)

Define another frame that holds the meaning label and another label that will display the meaning at the click of the Submit button. Place it in the frame you created above and set the appropriate font styles. Use the wraplength property to wrap a long sentence into multiple ones. Its dimension is set in screen units.

Organize and add some padding to the labels and the frames.

        frame1 = Frame(root)
Label(frame1, text="Meaning: ", font=("Arial 18 bold")).pack(side=LEFT)
meaning = Label(frame1, text="", font=("Arial 18"),wraplength=1000)
meaning.pack()
frame1.pack(pady=15)

Repeat the same steps for the synonym and antonym frames and labels.

        frame2 = Frame(root)
Label(frame2, text="Synonym: ", font=("Arial 18 bold")).pack(side=LEFT)
synonym = Label(frame2, text="", font=("Arial 18"), wraplength=1000)
synonym.pack()
frame2.pack(pady=15)

frame3 = Frame(root)
Label(frame3, text="Antonym: ", font=("Arial 18 bold")).pack(side=LEFT)
antonym = Label(frame3, text="", font=("Arial 18"), wraplength=1000)
antonym.pack(side=LEFT)
frame3.pack(pady=20)

Define a Submit button. Set the parent window you want to put the button in, the text it should display, the font style it should have, and the function it should run when clicked. The mainloop() function tells Python to run the Tkinter event loop and listen for events until you close the window.

        Button(root, text="Submit", font=("Arial 18 bold"), command=dict).pack()
root.mainloop()

Put all the code together and your Dictionary Application is ready to test.

Sample Output of the Dictionary App

When you run the above program it displays the app window. On entering a word, it displays the word’s meaning and a list of synonyms and antonyms.

Output of the dictionary app showing definition, synonyms, and antonyms for the word “happy”

Word-Based Applications Using Tkinter

Tkinter comes with classes for managing windows and widgets to build GUI applications. These include buttons, menus, text boxes, and text labels.

Once you’ve built it, you can convert your Python file to an executable to use it as a software application.