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 message boxes like showinfo, showerror, askyesno from tkinter.messagebox
6+ from tkinter .messagebox import showerror , askyesno
7+ # importing the PyDictionary library
8+ from PyDictionary import PyDictionary
9+ # this package converts text to speech
10+ import pyttsx3
11+
12+
13+ # the function for closing the application
14+ def close_window ():
15+ # this will ask the user whether to close or not
16+ # if the value is yes/True the window will close
17+ if askyesno (title = 'Close Audio Dictionary' , message = 'Are you sure you want to close the application?' ):
18+ # this destroys the window
19+ window .destroy ()
20+
21+
22+ # function for searching the word meaning
23+ def search_word ():
24+ # getting the word from the entry using the get()
25+ word = word_entry .get ()
26+ # checking if the word variable is empty
27+ if word == '' :
28+ # message box to display if the word variable is empty
29+ showerror (title = 'Error' , message = 'Please enter the word you wanna search for!!' )
30+ # the else statement will execute if the word variable is not empty
31+ else :
32+ # this will execute the code that will find the word meanings
33+ try :
34+ # creating a dictionary object
35+ dictionary = PyDictionary ()
36+ # passing a word to the dictionary object
37+ meanings = dictionary .meaning (word )
38+ # deleting content in the text field
39+ text_field .delete (1.0 , END )
40+ # inserting content(meanings) in the text field
41+ text_field .insert ('1.0' , meanings )
42+ # adding the word to the empty label
43+ word_label .config (text = word )
44+ # enabling the audio button to normal state
45+ audio_button .config (state = NORMAL , command = speak )
46+ # this will catch all the exceptions, No/slow internet connection, word with wrong spellings
47+ except :
48+ # display the error to the user
49+ showerror (title = 'Error' , message = 'An error occurred while trying to search word meaning' \
50+ '\n The following could be ' \
51+ 'the cause:\n ->No/Slow internet connection\n ' \
52+ '->Wrong word spelling\n ' \
53+ 'Please make sure you have a stable internet connection&\n the word spelling is correct' )
54+
55+
56+
57+
58+ # function to turn textual data into audio data
59+ def speak ():
60+ # getting the word from the entry
61+ word = word_entry .get ()
62+ # initializing the pyttsx3 object
63+ engine = pyttsx3 .init ()
64+ # gets the speaking rate
65+ rate = engine .getProperty ('rate' )
66+ # setting the speaking rate
67+ engine .setProperty ('rate' , 125 )
68+ # getting the available voices
69+ voices = engine .getProperty ('voices' )
70+ # seeting the second voice, the female voice
71+ engine .setProperty ('voice' , voices [1 ].id )
72+ # this function takes the word to be spoken
73+ engine .say (word )
74+ # this fucntion processes the voice commands
75+ engine .runAndWait ()
76+
77+
78+
79+ # creates the window using Tk() fucntion
80+ window = Tk ()
81+ # creates title for the window
82+ window .title ('Audio-Dictionary' )
83+ # this is for closing the window via the close_window() function
84+ window .protocol ('WM_DELETE_WINDOW' , close_window )
85+ # adding the window's icon
86+ window .iconbitmap (window , 'dictionary.ico' )
87+ # dimensions and position of the window
88+ window .geometry ('560x480+430+180' )
89+ # makes the window non-resizable
90+ window .resizable (height = FALSE , width = FALSE )
91+
92+ """Styles for the widgets"""
93+ # style for the big text label
94+ big_label_style = ttk .Style ()
95+ big_label_style .configure ('big_label_style.TLabel' , foreground = '#000000' , font = ('OCR A Extended' , 40 ))
96+ # style for small text labels
97+ small_label_style = ttk .Style ()
98+ small_label_style .configure ('small_label_style.TLabel' , foreground = '#000000' , font = ('OCR A Extended' , 15 ))
99+ # style for the entry
100+ entry_style = ttk .Style ()
101+ entry_style .configure ('TEntry' , font = ('Dotum' , 20 ))
102+ # style for the two buttons
103+ button_style = ttk .Style ()
104+ button_style .configure ('TButton' , foreground = '#000000' , font = 'DotumChe' )
105+
106+ # creates the canvas for containing all the widgets
107+ canvas = Canvas (window , width = 480 , height = 560 )
108+ # packing the canvas
109+ canvas .pack ()
110+ # creating a ttk label
111+ text_label = ttk .Label (window , text = 'Audio Dictionary' , style = 'big_label_style.TLabel' )
112+ # adding the label to the canvas
113+ canvas .create_window (250 , 55 , window = text_label )
114+ # creating a ttk entry
115+ word_entry = ttk .Entry (window , width = 73 , style = 'TEntry' )
116+ # adding the entry to the canvas
117+ canvas .create_window (230 , 110 , window = word_entry , height = 35 )
118+ # loading the icon
119+ search_icon = PhotoImage (file = 'search.png' )
120+ # creates dimensions of the icon
121+ logo = search_icon .subsample (20 , 20 )
122+ # creating a ttk button with a search icon
123+ search_button = ttk .Button (window , image = logo , style = 'TButton' , command = search_word )
124+ # adding the entry to the canvas
125+ canvas .create_window (468 , 110 , window = search_button )
126+ # loading the icon
127+ audio_icon = PhotoImage (file = 'speaker.png' )
128+ # creates dimensions of the logo
129+ icon = audio_icon .subsample (10 , 10 )
130+ word_label = ttk .Label (window , style = 'small_label_style.TLabel' )
131+ # adding the label to the canvas
132+ canvas .create_window (80 , 145 , window = word_label )
133+ # creating another ttk button with a speaker icon
134+ audio_button = ttk .Button (window , image = icon , style = 'TButton' , state = DISABLED )
135+ # adding the entry to the canvas
136+ canvas .create_window (25 , 190 , window = audio_button )
137+ # creating the text field
138+ text_field = Text (window , height = 15 , width = 60 )
139+ # adding the text field to the canvas
140+ canvas .create_window (248 , 340 , window = text_field )
141+ # runs the window infinitely
142+ window .mainloop ()
0 commit comments