How to make graphical Python apps the EasyGUI way

Easygui
(Image credit: Tom's Hardware)

When it comes to making GUI (Graphical User Interface) applications with Python, we are truly spoilt for choice. We have Tkinter, PyGame, GUIZero, and my personal favourite, EasyGUI.

EasyGUI is old; I was using it to teach Python back in 2015! But, as the name suggests, it's easy to use, and that is why I am still using it over a decade later. It just works across multiple operating systems, and I don’t need to get bogged down in the details of specifying the size and position of a dialog. I just tell EasyGUI that I want a specific type of dialog, and it creates it using the title, message, and interface that I specify. You may be heading to the comments to tell me that PyGame, GUIZero, or some other GUI toolkit is better. If so, please do share your knowledge. We learn by sharing, and I would love to hear your preference.

I’ve taken a few screenshots to illustrate.

To see all of the options, we first need to install EasyGUI. For this how to you will need Python installed on your computer. We’ve got a guide for Windows machines, but Linux users should already have it installed.

1. Open a Command Prompt / Terminal. Windows users can find this via the Start menu.

2. Use pip to install EasyGUI. Pip is Python’s built-in package manager.

pip.exe install easygui

3. Start the Python interpreter.

python -i

4. Import the EasyGUI module. I shortened the name of EasyGUI to eg when I imported the module. It makes it easier to use.

import easygui as eg

5. Call the demo function to see all of the different dialog options. Close the dialog window to end the demo.

eg.egdemo()

To create a dialog, first specify the type of dialog, then pass the parameters. Take, for example, this simple button box which has a title, msg, and choice. The title refers to the dialog’s title, the msg is the message to the user, and the choices are a Python list of options, which become buttons. Clicking on a choice will print the choice to the Python shell, or we can save it to a variable for later use.

Easygui

(Image credit: Tom's Hardware)

The Project: A To-Do list in Python

Easygui

(Image credit: Tom's Hardware)

Our goal is simple: create a to-do app that will keep us on track. The app will be written in Python, and use a GUI to make it easier for the end user. The to-do list will be saved to an external file using JSON. Python can easily work with JSON, thanks to the JSON module.

So what are we going to do?

  • Download and install the EasyGUI Python module.
  • Write Python code in a text editor.
  • Import the EasyGUI Python module, along with modules to write the tasks to a file using JSON format.
  • Create functions to handle viewing, creating and deleting tasks.
  • Test that the app works.

I’m going to assume that you have Python and the EasyGUI module installed, so let's open a text editor and start writing code. I’ll be using Notepad++ on my Windows 10 PC.

1. Open a text editor and create a new blank file. Save the file as to-do.py and remember to save often. I’m using Notepad++, but any text editor will do the job.

2. Import the easygui module and then the modules for using JSON and working with the underlying OS. Remember to rename EasyGUI as you import it for an easier means of calling the module.

import easygui as eg
import json
import os

3. Create a variable called “filename” to store the name of the JSON file that contains the to-do list. This can be a file anywhere on your system, but in this case it is a file in the same directory as the to-do.py file.

filename = "todo_list.json"

4. Create a function to load the to-do tasks saved in the JSON file. If the file exists (os.path.exists), then the function will return the contents. If there is no file, then it will return a blank list ( [ ] ).

def load_tasks():
    if os.path.exists(filename):
        with open(filename, "r") as f:
            return json.load(f)
    else:
        return []

5. Create a function to save the to-do tasks into the JSON file. We open the file and overwrite the existing contents with the new list, which is formatted into a JSON structure.

def save_tasks(tasks):
    with open(filename, "w") as f:
        json.dump(tasks, f)


6. Call the load_tasks function and store the output (what the Python function returns) into a variable called tasks. The load_tasks function will either return the existing to-dos stored in the JSON file, or create a blank Python list.

tasks = load_tasks()


7. Create a while True loop to run the code until the user exits.

while True:


8. Using EasyGUI, create a variable called “choice” and in there we store the user’s response to a button box. The first line is the text that is displayed to the user, note the \n which is Python for new line. The second line is the title of the dialog box. Image can be any GIF image that you choose. Finally, the choices are stored in a Python list, and they become clickable buttons that return the chosen value, stored in the variable “choice.”

  choice = eg.buttonbox(
        msg="What would you like to do?\nSelect an option below",
        title="To-Do List",
        image="clipboard.gif",
        choices=["View Tasks", "Add Task", "Remove Task", "Exit"]
    )



9. Create a conditional test, based on the user’s response to the button box. If their choice was to view the current tasks and there are tasks in the JSON file, display them using an EasyGUI textbox. Else, tell the user that there are no tasks to display, again using a textbox.

    if choice == "View Tasks":
        if tasks:
            eg.textbox("Your Tasks:", "To-Do List", "\n".join(tasks))
        else:
            eg.msgbox("No tasks yet!", "To-Do List")




10. Create the second choice in the conditional test which activates if the user selects “Add Task” from the button box. This creates a variable, task, that stores the user’s input via an EasyGUI enterbox. Then it appends the tasks object (a list) and calls the save_tasks functions to write the list into the JSON file. Finally, a message box confirms that the tasks have been added.

   elif choice == "Add Task":
        task = eg.enterbox("Enter a new task:", "Add Task")
        if task:
            tasks.append(task)
            save_tasks(tasks)
            eg.msgbox("Task added!", "To-Do List")





11. Create the next choice, which activates when a user selects to “Remove Task”. If there are tasks to remove, an EasyGUI choicebox will advise the user to select a task for removal. Then a nested if condition will remove the task from the task list (a Python list of the tasks), and then tell the user that the task has been removed. If there are no tasks to remove, then an EasyGUI message box will tell the user.

   elif choice == "Add Task":
        task = eg.enterbox("Enter a new task:", "Add Task")
        if task:
            tasks.append(task)
            save_tasks(tasks)
            eg.msgbox("Task added!", "To-Do List")





12. Using an else condition, create a break in the code to exit the app. This is effectively our quit / exit button.

   else:
        break

13. Save the code.

14. Open a Command Prompt window and navigate to the location of the to-do app.

15. Run the to-do app using Python and test out the functions of the app. You’ve just built a graphical application using EasyGUI and Python.

python to-do.py

If you want to make a true GUI application, one that has its own icon and doesn’t need the Command Prompt to run, then follow this guide and use your to-do.py file instead of the example code.

Complete Code Listing

import easygui as eg
import json
import os

filename = "todo_list.json"

def load_tasks():
    if os.path.exists(filename):
        with open(filename, "r") as f:
            return json.load(f)
    else:
        return []

def save_tasks(tasks):
    with open(filename, "w") as f:
        json.dump(tasks, f)

tasks = load_tasks()

while True:
    choice = eg.buttonbox(
        msg="What would you like to do?\nSelect an option below",
        title="To-Do List",
        image="clipboard.gif",
        choices=["View Tasks", "Add Task", "Remove Task", "Exit"]
    )

    if choice == "View Tasks":
        if tasks:
            print(tasks)
            eg.textbox("Your Tasks:", "To-Do List", "\n".join(tasks))
        else:
            eg.msgbox("No tasks yet!", "To-Do List")

    elif choice == "Add Task":
        task = eg.enterbox("Enter a new task:", "Add Task")
        if task:
            tasks.append(task)
            save_tasks(tasks)
            eg.msgbox("Task added!", "To-Do List")

    elif choice == "Remove Task":
        if tasks:
            task_to_remove = eg.choicebox(
                "Select a task to remove:",
                "Remove Task",
                tasks
            )
            if task_to_remove:
                tasks.remove(task_to_remove)
                save_tasks(tasks)
                eg.msgbox("Task removed!", "To-Do List")
        else:
            eg.msgbox("No tasks to remove!", "To-Do List")

    else:
        break
TOPICS
Les Pounder

Les Pounder is an associate editor at Tom's Hardware. He is a creative technologist and for seven years has created projects to educate and inspire minds both young and old. He has worked with the Raspberry Pi Foundation to write and deliver their teacher training program "Picademy".