Tkinter and MongoDB: How to build a GUI application with data from MongoDB and MySQL using Python

Let’s learn python programming concepts by developing a GUI application to search and display data from NoSQL and SQL databases.

Saurabh Ghosh
Predict
8 min readFeb 3, 2023

--

Photo by Myke Simon on Unsplash

What to expect in this blog?

In my previous blog, I explored how to retrieve movie records from a popular website and store the details in MongoDB and MySQL databases.

In this blog, you’ll be exploring two primary areas in this blog -

  • Database operations — You’ll connect to a mix of NoSQL and SQL databases to retrieve records. For the NoSQL database, you’ll use MongoDB and PyMongo libraries of Python. Additionally, you’ll use the MySQL database and MySQL connector libraries of Python.
  • GUI application development — Retrieving data only is boring. So, you’ll create a GUI application with the Tkinter library to search for movie details with criteria like genre, director, writer and actor.
Screenshot from sample application execution

Some key points you’ll be exploring with this code -

  1. Using PyMongo library methods to search and retrieve documents
  2. Performing a search on MongoDB collection with “and”, “or” conditions
  3. Processing “dictionary” documents retrieved from MongoDB collection
  4. Using MySQL library methods to retrieve records
  5. Using the Tkinter library and components e.g. combo box, button, text.
  6. Using a Treeview control of Tkinter to print table rows
  7. Using a Scrollbar control of Tkinter with Treeview and Text controls
  8. Execute Python script alone or as a module
  9. Python class, methods and variables
  10. Using error handling with try and except

Let’s plan for the work

Before starting the design and coding, let’s understand the requirement for the application.

User interaction involved

  1. Users should be able to select a genre (e.g. Action, Adventure etc.) from a list and submit a request to show the top 50 movies in the selected genre.
  2. A separate section should appear to show the list of movies.
  3. Users should be able to filter this list of movies with names of directors, writers and actors.
  4. After filter criteria are applied, the list of movies should show the matching movies with the criteria.
  5. Users should be able to choose any movie in the list to view details of the movie.
  6. Details of the selected movie should be shown in a separate section.
  7. Users should be able to copy text content from the details section.

Essential processing involved supporting the user interaction

  1. At the start of the application, the list of directors, writers and actors should be loaded in the application memory.
  2. When the top 50 movies are to be shown for a selected genre, the application should retrieve all the documents stored in the MongoDB collection for the genre.
  3. When users select filtering criteria to narrow the list, the application should perform a filtered search in the MongoDB collection to fetch the matching list of movies.
  4. The selection of any movie from the list should trigger an event to retrieve details of the selected movie only from the MongoDB collection.
  5. The details of the selected movie will be retrieved as a ‘dictionary’ object with ‘list’ objects within it. The application should format the ‘dictionary’ object details to show in a nicely formatted manner.

High-level design thinking

Now you can plan the required classes, methods and attributes.

DBHandler class

You need a class to handle the interaction with the database. This is necessary so that you do not need to update your scraper code if there is a change in database connectivity or logic. For example, if you need to interact with a different database in future (maybe Oracle instead of MySQL or ArangoDB instead of MongoDB), you’ll only update your DBHandler class.

This class will have below methods -

  1. __init__() — This will create the connection to the MongoDB and MySQL databases before any other operation.
  2. get_directors() — This method will retrieve the names of directors from the MySQL table that contains roles and names.
  3. get_writers() — This method will retrieve the names of writers from the MySQL table that contains roles and names.
  4. get_actors() — This method will retrieve the names of actors from the MySQL table that contains roles and names.
  5. get_movies_results() — This method will be called any time the list of movies needs to be retrieved. It’ll take the genre as mandatory input (to determine the MongoDB collection) and take the names of the director, writer and actor as optional parameters. This method will formulate the search query for MongoDB and get the list of ‘dictionary’ objects from the database.
  6. get_movie_detail() — This method will search with the title passed as a parameter. Since the assumption is that the top 50 movies in one genre won’t have duplicate titles, it’s expecting to get one ‘dictionary’ object only.
  7. close_connections() — This method will be called at end of the program to close the connections with MongoDB and MySQL.

MovieFinder class

This class will handle the operations related to showing the screen elements and handling user input.

This class will have below methods -

  1. __init__() — This constructor method will initialize the list of genres with a static array. This will also initialize the DBHandler instance and retrieve the list of directors, writers and actors.
  2. show_fields() — This is the first method that needs to be called. This method will render the list of genres as a combo box and place a button to submit the request.
  3. show_movies() — This is a busy method. It will be called on the below occasions — (a) when retrieving the list of all top 50 movies in a genre, (b) when the filtering criteria are selected and the user wants to see the filtered list. It’ll render the filtering dropdowns. It’ll display a Treeview control with the list retrieved from the ‘get_movies_results()’ method of DBHandler class. This method will have an inner method (item_selected()) to attach to the selection event of the Treeview control.
  4. item_selected() — This is an inner method attached to the ‘TreeviewSelect’ event. This method will call the ‘get_movie_detail()’ method of the DBHandler class to get the movie detail as a ‘dictionary’ object. It’ll then call a global method ‘format_movie_detail()’ to get the formatted text to show in a multiline Text element.

Global methods and attributes

Apart from the above classes and methods, the application module will contain below global attributes and methods -

format_movie_detail() — This method will take a ‘dictionary’ object as a parameter and traverse through the key-value pairs. It’ll add the key and value pairs in a properly separated format. If the value is a list, it’ll add the values in the list as comma-separated values.

Let’s code

Now that you know the main methods and their purpose, now let’s get started.

You can follow the comments and docstrings within the code snippets.

“DBHandler” class

Import, class definition and __init__() method

Key points —
— Connecting to a MongoDB instance.
— Connecting to a MySQL instance.
— Catching and handling exception

get_directors()

Key points —
— Using MySQL library methods to retrieve records

get_writers()

get_actors()

get_movies_results()

Key points —
— Using PyMongo library methods to search and retrieve documents
— Performing a search on MongoDB collection with “and”, “or” conditions

get_movie_detail()

close_connections()

Application module

This module will contain the MovieFinder class and the global elements for the application.

Application imports

“MovieFinder” class

Class definition and __init__() method

show_fields()

Key points —
— Using the Tkinter library and components e.g. combo box, button.

show_movies()

The below method contains the inner method ‘item_selected()’ as well.

Key points —
— Using the Tkinter library and components e.g. combo box, button.
— Using a Treeview control of Tkinter to print table rows.
— Using a Scrollbar control of Tkinter with Treeview control.

item_selected()

This method is present within the ‘show_movies()’ method as well. Showing it here separately.

Key points —
— Using the Tkinter library and components e.g. Text control.
— Using a Scrollbar control of Tkinter with Text control.

Global methods

Key points —
— Iterating through key and value pairs in a ‘dictionary’ object

Starting/Invoking the application

This is written within the application module (the “moviefinder.py” file in GitHub), however outside the class.
Notice the check for “if __name__ == ‘__main__’:”. This will ensure that the code with this condition is executed only when run directly by the “python -m <<package name>> moviefinder.py” command.

If you want to make a package for the application e.g. “boxoffice”, the above code will go into the “__main__.py” file in the package. And the command will be -

python -m boxoffice moviefinder.py

That’s all the coding.

Run the program

The first screen after “python -m boxoffice movifinder.py” -

Select a genre and click on the button.

The application will show the top 50 movies in a Treeview control along with three combo boxes for the filter criteria.

You can select any movie in the list to view the details of that movie. It’ll show the details in a Text control.

You can apply a filter in the list by selecting one of the criteria e.g. director, writer and actor names and click search.

Close the window any time to exit the application.

Happy coding!!

Download

GitHub — https://github.com/SaurabhGhosh/MovieFinder_MongoDB_to_Tkinter.git

Conclusion

In this blog, I hope you got some ideas about below -

  1. Using PyMongo library methods to search and retrieve documents
  2. Performing a search on MongoDB collection with “and”, “or” conditions
  3. Processing “dictionary” documents retrieved from MongoDB collection
  4. Using MySQL library methods to retrieve records
  5. Using the Tkinter library and components e.g. combo box, button, text.
  6. Using a Treeview control of Tkinter to print table rows
  7. Using a Scrollbar control of Tkinter with Treeview and Text controls
  8. Execute Python script alone or as a module
  9. Python class, methods and variables
  10. Using error handling with try and except

In my next blog, I’ll explore another program and learn more concepts.

If you have any questions related to this program, please feel free to post your comments.

Please like, comment and follow me! Keep Learning!

--

--

Saurabh Ghosh
Predict

Business Analyst, Machine Learning Enthusiast, Blogger