Make Simple GUIs with the Raspberry Pi using EasyGUI

Make an Interface for your Project

EasyGUI Message Boxes
EasyGUI is a quick and simple project to try this weekend. Richard Saville

Adding a graphical user interface (GUI) to your Raspberry Pi project is a great way to include a screen for data entry, on-screen buttons for controls or even just a smarter way to show readings from components such as sensors.

There are a number of different GUI methods available for the Raspberry Pi, however most have a steep learning curve. 

The Tkinter Python interface may be the default 'go to' option for most, however beginners can struggle with its complexity. Similarly the PyGame library offers options for making impressive interfaces, but may be surplus to requirements.

If you're looking for a simple and quick interface for your project, EasyGUI could be the answer. What it lacks in graphical beauty it more than makes up for in its simplicity and ease of use.

This article will give you an introduction to the library, including some of the most useful options I've found.

Downloading and Importing EasyGUI

EasyGUI apt-get install
EasyGUI installation is simple with the 'apt-get install' method. Richard Saville

For this article I'm using the standard Raspbian operating system which is available here.

Installing the library will be a familiar process to most, using the 'apt-get install' method. You'll need an internet connection on your Raspberry Pi, using either a wired Ethernet or WiFi connection.

Open a terminal window (the icon of a black screen on your Pi's taskbar) and enter the following command:

apt-get install python-easygui

This command will download the library and install it for you, and that's all the setup you need to do.

Import EasyGUI

Import EasyGUI
Importing EasyGUI takes just one line. Richard Saville

EasyGUI needs to be imported into a script before you can use its functions. This is achieved by entering a single line at the top of your script, and is the same regardless of which EasyGUI interface options you use.

Create a new script by entering the following command in your terminal window:

sudo nano

A blank screen will appear - this is your empty file (nano is simply the name of a text editor). To import EasyGUI into your script, enter the following line:

from easygui import *

We use this specific version of the import to make coding even easier later on. For example, when importing this eway, instead of having to write 'easygui.msgbox' we can simply use 'msgbox'.

Now let's cover some of the key interface options within EasyGUI.

Basic Message Box

EasyGUI Message Box
The simple message box is a great way to start with EasyGUI. Richard Saville

This message box, in its simplest form, gives the user a line of text and a single button to click. Here's an example to try - enter the following line after your import line, and save using Ctrl+X:

msgbox("Cool box huh?", "I am a Message Box")

To run the script, use the following command:

sudo python

You should see a message box appear, with 'I am a Message Box' written in the top bar, and 'Cool box huh?' above the button.

Continue or Cancel Box

EasyGUI Continue Cancel Box
The Continue/Cancel box can add confirmation to your projects. Richard Saville

Sometimes you will need to the user to confirm an action, or choose whether or not to continue. The 'ccbox' box offers the same line of text as the basic message box above, but provides 2 buttons - 'Continue' and 'Cancel'.

Here's an example of one in use, with the continue and cancel buttons printing to the terminal. You could change the action after each button press to do whatever you like:

from easygui import *
import time
msg = "Would you like to continue?"
title = "Continue?"

if ccbox(msg, title):     # show a Continue/Cancel dialog
    print "User selected continue"
    # Add other commands here
else:  # user chose Cancel
    print "User cancelled"
    # Add other commands here

Custom Button Box

EasyGUI Buttonbox
The 'buttonbox' allows you to make custom button options. Richard Savlle

If the built-in box options aren't quite giving you what you need, you can create a custom button box using the 'buttonbox' feature.

This is great if you have more options that need covering, or perhaps are controlling a number of LEDs or other components with the UI.

Here's an example selecting sauce for an order:

from easygui import *
import time msg = "Which sauce would you like?"
choices = ["Mild","Hot","Extra Hot"]
reply = buttonbox(msg, choices=choices) if reply == "Mild":
    print reply

if reply == "Hot":
    print reply

if reply == "Extra Hot":
    print reply

Choice Box

EasyGUI Choice Box
The Choice Box is great for longer lists of items. Richard Saville

Buttons are great, but for long lists of options, a 'choice box' makes a lot of sense. Try fitting 10 buttons in a box and you'll soon agree!

These boxes list the available options in rows one after another, with an 'OK' and 'Cancel' box to the side. They're reasonably smart, sorting the options alphabetically and also allowing you to press a key to jump to the first option of that letter.

Here's an example showing ten names, which you can see has been sorted in the screenshot.

from easygui import *
import time
msg ="Who let the dogs out?"
title = "Missing Dogs"
choices = ["Alex", "Cat", "Michael", "James", "Albert", "Phil", "Yasmin", "Frank", "Tim", "Hannah"]
choice = choicebox(msg, title, choices)

Data Entry Box

EasyGUI Multenterbox
The 'Multenterbox' lets you capture data from users. Richard Saville

Forms are a great way to capture data for your project, and EasyGUI has a 'multenterbox' option that allows you to show labelled fields to capture information with.

Once again it's a case of labelling fields and simply capturing the input. I've made an example below for a very simple gym membership sign-up form.

There are options to add validation and other advanced features, which the EasyGUI website covers in detail.

from easygui import *
import time
msg = "Member Information"
title = "Gym Membership Form"
fieldNames = ["First Name","Surname","Age","Weight"]
fieldValues = []  # the starting values
fieldValues = multenterbox(msg,title, fieldNames)
print fieldValues

Adding Images

EasyGUI Images
Add images to your boxes for a whole new way to use the GUI. Richard Saville

You can add images to your EasyGUI interfaces by including a very small amount of code.

Save an image to your Raspberry Pi in the same directory as your EasyGUI script and make a note of the file name and extension (for example, image1.png).

Let's use the button box as an example:

from easygui import *
import time
image = "RaspberryPi.jpg"
msg = "Is this a Raspberry Pi?"
choices = ["Yes","No"]
reply = buttonbox(msg, image=image, choices=choices)

if reply == "Yes":
    print "Yes"

    print "No"

More Advanced Features

EasyGUI Password
You can't make payment systems with EasyGUI, but you can have fun pretending!. Richard Saville

I've covered the main 'basic' EasyGUI options here to get you started, however there are lots more box options and examples available depending on how much you want to learn, and what your project requires.

Password boxes, code boxes and even file boxes are available to name a few. It's a very versatile library that's easy to pick up in minutes, with some great hardware control possibilities as well.