DEV Community

Cover image for GUI Dictionary using api calls in python (with pronunciation)
Swapan
Swapan

Posted on • Updated on

GUI Dictionary using api calls in python (with pronunciation)

Dictionary is defined by Merriam-Webster as “a reference book listing alphabetically the words of one language and showing their meanings or translations in another language”. Using python I’m creating a real-time dictionary with pronunciation.

Below is the concept:

  1. User API calls to pull the data. (I’m using Oxford Dictionaries - here) in this example.
  2. Understand the data that has been pulled in – , list or dict.
  3. Understand the type of data we need and extract it.
  4. Create a UI screen with buttons like “Get Meaning”, to fetch the meaning of the word, “Pronunciation” , to play sound and “Clear”, to erase the content and make room to request for new

Dependencies:

  1. Using Python 3.9 (Latest version) -download here
  2. Use Tkinter for GUI (pip install tkinter)
  3. Use requests for pull data via API call (pip install requests
  4. Use JSON for parsing, slicing and dicing api data in JSON format ( pip install JSON)
  5. User playsound for playing sound (.mp3 in our case) (pip install playsound)

Final Output

When application loads

Showing the meaning of the word
With Meaning

Sunny side up
Output in Spanish (language_code = "es")
Spanish
Output in French (language_code = "fr")
French
List of Languages Supported -here

Updated UI
Updated UI

Code Snippet

   # Constants 
   appid  = "Your AppID"
   appkey  = "Your Key"
   headers = {'app_id': app_id, 'app_key': app_key}
   language_code = "en-us" # Change language if you want to use 
a different once

 # Pull data via URL
 url = "https://od- 
 api.oxforddictionaries.com/api/v2/entries/" 
 + language_code + "/" + getword.lower()

r_status = requests.get(url, headers=headers)

# This checks if the api is live or not <Response [200]>
  print(r_status)

# Additional varibles for readability
r_mean = r_status
r_audio = r_mean
Enter fullscreen mode Exit fullscreen mode

Buttons

# Create a button widget
    # The get_word() 
    b1 = Button(window,text="Get Meaning",command=get_word)
    b1.grid(row=0,column=2)

   # The delete function is called when the button is pushed
   b2 = Button(window,text="Clear",command=delete)
   b2.grid(row=1,column=2)

   # The get_audio function is called when the button is 
   pushed
   b3 =Button(window,text="Pronunciations",command=get_audio)
   b3.grid(row=4,column=1)

  # Create four empty text boxes t1

  t1 = Text(window,height=2,width=45,font = ("Times New Roman",12) )
  t1.grid(row=3,column=1, pady = 5, padx = 2.5)
  t1.config(wrap=WORD)
Enter fullscreen mode Exit fullscreen mode

Functions

# Logic to pull meaning of the word 
        def get_word(): 
        #Multiple for loops increases the running time.

         for result in mean_json['results'][0]['lexicalEntries'][0]['entries']:
    for sense in result['senses']:
        mean_list.append(sense['definitions'][0])

   for i in mean_list:
        return (i)




 # Logic to pull audio file
    def get_audio():
    for result in audio_json['results'][0]['lexicalEntries'][0]['entries']:
        for pronunciations in result['pronunciations']:
            audio_list.append(pronunciations)
Enter fullscreen mode Exit fullscreen mode

Code will soon be added to Github.
Note: Thanks to Tim Wildsmith for this photo via @unsplash 🎁

Top comments (0)