How to use easygui (part 1/3)
This is my first post, and it is going to be about gui programming in python. I am looking into tkinter – the “standard” gui programming tool that comes with python, but it is rather complex so I looked for a simpler way of doing things to start off with. The python package that I found was easygui.
The same package works regardless of whether you are running python 2 or 3. As it does not come with the default python install, we need to install it. The following instructions are for windows:
- Download and extract the contents of this zipped folder to the same directory as your python installation:
The python directory if you are using the portable version of python is the ‘App’ folder, where you should see files like “python.exe”, and “pythonw.exe”.
- Open the command prompt.
- In the command prompt, navigate to your python installation. For portable python this should be something like “
e:\portablepython\app“. This can be done with the “
cd” command: “
cd e:\portablepython\app“. Make sure that there are no spaces in the file path if you are using a school computer, as this can cause problems. If you do have spaces, simply move the startcmd.bat file to the app folder in your portable python installation.
- Type in “
python.exe setup.py install“. This will then install easy gui. This same method can be used to install any python package that has a setup.py file.
- Delete the two files that you extracted to the python install directory.
You should now have a working install of easygui! To test it, open the interactive interpreter and type the following:
import easygui as eg
This should bring up a box with some help on how to use easygui. Now onto how to use it!
Note:If you install easygui on a pi, don’t run scripts that use easygui from within IDLE. This is because easygui is a set of routines that run on top of tkinter, and IDLE runs through tkinter itself. Run scripts that use easygui from outside IDLE.
Firstly, we need to import easygui so that we can use it:
import easygui as eg
This imports easygui under the name
Easygui can allow you to do several things:
- Show a message box
- Get the user to select from several options
- Get the user to select options from a list
- Ask the user to type in one or more text boxes
- Ask the user for a password
- Show the file/folder open/save box
- Save settings between runs of your script
I will cover all of these things, but probably over several posts, as this one is getting long. The first thing I will talk about a message box. After importing easygui as described above, you can do this:
This should show a message box like this:
To set the title of the window, you have to add an extra parameter:
eg.msgbox("This is some awesome text", "Easygui is awesome!");
The above code should display the following:
To ask a yes/no question, do this:
eg.ynbox("This is the question", "This is the window title");
This should give you a popup like this:
You can get the value that the user picked by assigning it to a variable, so that you can use an if statement to perform different actions based on what you choose:
choice = eg.ynbox("This is the question", "This is the window title"); if choice == True: eg.msgbox("You picked yes"); else: eg.msgbox("You picked no");
To ask the user to pick from a selection of choices, we can store the choices in a list, and then ask the user as follows:
question="Which do you like the most?" choices = ["Cake", "Fish", "Pie"] title="Food"; choicepicked=eg.choicebox(question, title, choices);
This should give you a popup something like this:
choicepicked should now contain the contents of the choice you chose. In the case above, it should contain the string
Hopefully this post has been helpful, more coming in the next installment as this post is very long. Please leave a comment below because this is my first time posting!
Edit: The next post in this series is now available! You can view it here.