Skip to main content

English Dictionary With Python And Tkinter!

English Dictionary on Python

Have you ever tried to read from the small Oxford Dictionaries? Those small yet bulky books have thousands of words cramped on a page the length of your finger! And on top of that, not to forget the hassle of flipping across the pages, searching for your word, God knows where is hiding on which page!

Nowadays though, hardly does anyone ever use the classic dictionaries. With the access of technology on our fingertips, one tap on Google, and you would be on with your way.  But how about making a program of your own which can do the same for you? Sounds interesting?

Such a program can enable you to run it and keep it opened, while you are reading a book, so that you can search the definition of a new word you encountered. What if you are writing a book, perhaps a report? You know what you are supposed to write, but you choose to get a word for it, so that you sound professional. So you just search for a short definition and the program find the word with matching definition.

What if along with writing a book, you also require a way to get only one type of word, say only nouns? A program which takes any combination of above mentioned factors to give you matching results.

And a finishing touch: You wish to become a 'Word Enthusiast', and have your way with words? How about a subscription that sends you an email regularly with the definition of a new word.

That's what we are going to do today. And don't forget to download the entire python program from the link given in the end, free!

Downloading the Dictionary

We require a .csv file, which contains all the words in the English Dictionary. For this project, it shall contain the word, the type of word (noun, pronoun, preposition...) and the definition. I have downloaded this file: English Dictionary File (credits to Manas Sharma, taken from here: 

If you manage to find any other file with similar format, then no problem, yet if you proceed with this file, the code may not need any tweaks. Here's how the file looks:

As you can see, the first column consists of the words, the second has the type and the third the definition. Download this file wherever you prefer and copy its directory (along with file name) to declare it in the program. For example: C:\users\dictionary.csv

MySQL Part

For the program to send emails to those people who have subscribed to receive a new word with definition everyday, you need to create a database which stores the email ID of the subscribers. Create a Database called dictionary_subscriptions and create a table 'subscribers' using the following query:

    create table subscribers(email varchar(100) not null unique);

And that's it!

Required Modules/Libraries

We need pandas, imported as pd, to get the information from the .csv file using pd.read_csv() command. We will also import Tkinter for the user interface, along with messagebox which comes with Tkinter. mysql.connector for the Python-MySQL connection and smtplib, ssl for sending emails. We also require random and datetime, but these are pre-installed.

Program Overview

The program will let us input any combination of word, word type and word definition, and search the dictionary if the inputted word is a part of or the entire word is present in the dictionary, looping the entire file. Results will be posted on a new window with each window providing the matched results. You also get an option to subscribe or unsubscribe from receiving a new word everyday by email.

Below I will be explaining the code, one snippet at a time. You may skip it to the end to get the source code download link.

The Code

Importing the mentioned modules declaring a few variables as 'global' , which we will use across multiple functions:

Here df will be the pandas structure we will get from extracting our .csv file. font13 and font20wb will contain different fonts for use in the user interface. root will be the first Tkinter window, and words_per_page is going to specify how many results we will want in a page after we search for a word. From my testing, the ideal words_per_page is 5. We have two variables font13 and font20wb used as fonts for the labels. We have initiated the MySQL connection, and make sure you change the database argument to the name of the database you have created. And finally we have file_url which stores the complete directory of the .csv file we have downloaded. Replace it with the one where you have downloaded yours. 

From here on we will be using a lot of functions, to call each part of our program. User defined functions, made with the def function, are very useful and are helpful in passing values between different aspects of the program.

So our first function is the main_menu() function. This will be the first function to open on running the program and will serve aas the main screen, from where the user can search the desired words:

So we have declared root (the main tkinter window) as global. If you want, you can keep in mind that we did so, because I will explain why later on. We have two entry boxes each for getting the word, and the definition. We have a dropdown menu for the third factor, that is the word type. the 'options' list will hold the different options that appear on the dropdown menu for word type. The other stuff is just the positioning of these elements on the window. We also have the search button, which the user will press after filling up the desired inputs. The search button then connects to the second function search() with the user inputs.

Note that here and there you will find a label with text empty, and this is to provide extra lines of space between the elements.

Moving on with the second function, search():  

As I mentioned earlier, the program will display results that match with any combination of the three inputs. Depending on which field has been entered and which has not been entered, the program uses the if-else statements accordingly. On choosing the appropriate if-else statement, the program will get the indexes of all matching results, and put them in a list result_indexes. This searching method is really basic because it only uses the in statement, which is True if the user entered word is at least a part of a word, or False if it doesn't.

There is also a variable 'stat' which we initiate with value as 'false'. It only becomes 'true' if the result_indexes list has at least one value in it. Else, stat will remain 'false' and it will prevent the program from going to the results page, simply because it didn't find any words. And in that case, it will give a messagebox telling no words found.

And the last step if 'stat' becomes 'true' is we calculate a variable called last_page. This variable will decide what number (1, 2, 3 and so on) will the last page be, by dividing number of results by the words_per_page. We also perform an increment of 1 to get actual last_page in case the last_page comes to be 0. For those who can understand this, you can think of the calculation of last_page as Greatest Integer.

Moving on, our next function is results()  which will post the results for the user to see. Things will get pretty complicated here. Our search()  is sending two arguments result_indexes which is the list of indexes of matched words, and 1, which I will explain in the next function:

results() is taking two arguments: result_indexes and page. result_indexes is the same list that we prepared, and page, which is a variable containing the page number of results, initiated from the last function. Since we are taking only as many number of words in a single page as in words_per_page, we will require multiple pages. This variable page, as a result will start from 1, and every time the user choses to see the next set of results, the value will get increased by 1, so that the function posts the next set of words. This will go on until page == last_page.

We also have a title_label which will show us which page we are on. We have initiated a variable n from 1, which is purely used for label placement, so you can ignore that stuff. We have a for loop for placing each word, word type, and its definition on the page. There are if-elif statements governing when the number of words posted have reached the words_per_page limit.

From what I had seen, some definitions were very long which made it posting them on a single line look unprofessional. So internally, you can see: for m in range(0, len(defi)) which is adding \n after a particular number of words have been inputted (mentioned as [20, 40, 60, 80, 100]). This \n signals the program to write the trailing words on the next line.

If you remember, I had stated earlier that we have stated root to be global. If you have noticed, the results page also uses the same root. Now here's something to remember: It is usually advised to use top = Toplevel() for every new window you open, instead of root. But in this case, I have used root for results page as well. This is because, with every click of the next page button we will provide, we are telling the program to destroy the current results page, open a new page and show the next set of results. If you use top = Toplevel(), the program fails to understand which window it had to  destroy. Therefore, we had declared root global earlier, and now we are using the same for the results pages.

We have so many buttons for this page! We have buttons for going to the next page, or the previous page, which on clicked, restarts this entire function with page+1 or page-1 respectively. This makes the process convenient. There is a button for jumping back to the main menu, which access a small function called back_to_menu() which simply destroys the results page and opens the main_menu() function, so that the user can search for a different word. And then there are two buttons for jumping to the last page or the first page, which again restarts the function with the appropriate value for page. The page turning buttons are also made to become disabled, when they shouldn't be clicked, for example, when the user is on the last page, the jump to last page and next page buttons get disabled. Rest of the stuff is just the placement of different elements on the page.

Moving on to our next functions, subscibe() and to_unsubscribe(). Both of them are connected from the main menu from a single button with text 'subscribe'. Both of them have single entry fields each that ask the user for their email ID, to subscribe for the daily learning. They have submit buttons connecting to other functions to_submit() and submit2() which are passing the queries to add or delete the email IDs from the MySQL database, whose connection we have initiated on the top of the program. Here's their code:

Coming to the last function, daily_learning():

So we are fetching all the email IDs from our database, getting a random word, and sending them via smtp, the code for which you should copy as is, with tweaks you may want for the message, and also change the sender's email ID and password with that of yours. Also, you need to sign in to google with this account and turn on Allow Less Secure Apps for the email to be sent. Otherwise, Python will show you the error that the permission was denied.

In the end, at the bottom of the entire code, all you have to write is this:

that is, we are initiating the functions daily_learning() and main_menu() when the program is run, thus sending the emails to the subscribers as well as opening the main menu.

And that's it. We have completed our program and it is ready for use. Read below the summary of tweaks that you have to do to run it.

Your Tweaks

  1. Change the MySQL database argument in the mycon = mc.connect(.... with the name of the database that you have created for the purpose (for the subscribers).
  2. My table in my database is called subscribers. If you have created a table with a different name, replace subscribers with the name of your table under the functions to_submit() and submit2().
  3. Change file_url with that of the directory where you have downloaded your .csv file on your pc.
  4. Change the sender's email and password with that of yours under the function daily_learning().
And that's it! With these minor tweaks, you are now ready to run the program for yourself. Continue Reading to see how it performs.


Here's a video of how it works:

And here's the Email that the subscribers get:

So we have reached the end of this project! Look below for the download link of this project! Don't forget to go over the tweaks you have to do before running it yourself, as discussed above. Enjoy!

 Download Python Program


Popular posts from this blog

Simple Omegle Bot Using Selenium With Python

Omegle is an online text-based and video-based chatting platform, which allows users from around the world to talk to complete strangers anonymously, for free! The text-based Omegle has a simple concept: Complete a Captcha  verification, connect automatically to a stranger, and after chatting get on to the next stranger. Apart from having a chit-chat with a stranger from the far side of the globe, Omegle poses as the perfect platform for other uses as well. At Omegle, you  can advertise your content, website, products and more for free. With access to about 40,000 strangers using Omegle at any given time, you can benefit if your ideas/advertisement is seen by potential customers. But of course, we can't advertise to each guy we meet again and again. I mean, come on, that's a lot of hard work, even if your message consists of a few words. But worry not, since that's where our Omegle Bot  comes in play. This bot works on a pre-determined set of messages that are to be conveye

Predicting Stock Prices using Machine Learning (XGBoost)

  Today, I'll show you how to create a machine learning program to predict stock prices. Machine learning is used in a variety of fields, and we can utilize its ability to learn and predict from data to predict useful variables, with minimal error. Therefore in theory, we can apply the same on stocks to predict the next closing price, so that we can make a killing gain. However, stock markets are highly unstable. Their price movement often depends on decisions taken by the company, favor and reaction of investors, social impact, human emotions, and price movements of some other related stocks. These types of data cannot be made available to a program. The prediction can be close only if the market remains relatively stable. You don't actually need to learn machine learning using python to understand how this program works, if not minimal. I'll describe each machine learning process. And don't forget to download the source code of this program, link provided at the end.