You may encounter problems using IDLE to run programs that use easygui. Try it and find out. easygui is a collection of Tkinter routines that run their own event loops. IDLE is also a Tkinter application, with its own event loop. The two may conflict, with the unpredictable results. If you find that you have problems, try running your program outside of IDLE.
In easygui, all GUI interactions are invoked by simple function calls.
Here is a simple demo program using easygui. The screens that it produces are shown on the easygui home page.
from easygui import * import sys while 1: msgbox("Hello, world!") msg ="What is your favorite flavor?" title = "Ice Cream Survey" choices = ["Vanilla", "Chocolate", "Strawberry", "Rocky Road"] choice = choicebox(msg, title, choices) # note that we convert choice to string, in case # the user cancelled the choice, and we got None. msgbox("You chose: " + str(choice), "Survey Result") msg = "Do you want to continue?" title = "Please Confirm" if ccbox(msg, title): # show a Continue/Cancel dialog pass # user chose Continue else: sys.exit(0) # user chose Cancel
To run EasyGui's demonstration routine, invoke easygui from the command line this way:
python easygui.py
or from an IDE (such as IDLE, PythonWin, Wing, etc.) this way:
import easygui as g g._test()
This will allow you to try out the various easygui functions, and will print the results of your choices to the console.
In order to use easygui, you must import it. The simplest import statment is:
import easygui
If you use this form, then to access the easygui functions, you must prefix them with the name "easygui", this way:
easygui.msgbox(...)
One alternative is to import easygui this way:
from easygui import *
This makes it easier to invoke the easygui functions; you won't have to prefix the function names with "easygui". You can just code something like this:
msgbox(...)
A third alternative is to use something like the following import statement:
import easygui as g
This allows you to keep the easygui namespace separate with a minimal amount of typing. You can access easgui functions like this:
g.msgbox(...)
Once your module has imported easygui, GUI operations are a simple a matter of invoking easygui functions with a few parameters. For example, using easygui, the famous "Hello, world!" program looks like this:
from easygui import * msgbox("Hello, world!")
To see a demo of what easygui output looks like, invoke easygui from the command line,this way:
python easygui.py
To see examples of code that invokes the easygui functions, look at the demonstration code at the end of easygui.py.
For all of the boxes, the first two arguments are for message and title, in that order. In some cases, this might not be the most user-friendly arrangment (for example, the dialogs for getting directory and filenames ignore the message argument), but I felt that keeping this consistent across all widgets was a consideration that is more important.
Most arguments to easygui functions have defaults. Almost all of the boxes display a message and a title. The title defaults to the empty string, and the message usually has a simple default.
This makes it is possible to specify as few arguments as you need in order to get the result that you want. For instance, the title argument to msgbox is optional, so you can call msgbox specifying only a message, this way:
msgbox("Danger, Will Robinson!")
or specifying a message and a title, this way:
msgbox("Danger, Will Robinson!", "Warning!")
On the various types of buttonbox, the default message is "Shall I continue?", so you can (if you wish) invoke them without arguments at all. Here we invoke ccbox (the close/cancel box, which returns a boolean value) without any arguments at all.
if ccbox(): pass # user chose to continue else: return # user chose to cancel
It is possible to use keyword arguments when calling easygui functions. (Prior to version 0.80, only the use of positional arguments was documented. In version 0.80, parameter names were modified to be more consistent, and the use of keyword arguments was documented.)
Suppose for instance that you wanted to use a buttonbox, but (for whatever reason) did not want to specify the title (second) positional argument. You could still specify the choices argument (the third argument) using a keyword, this way:
choices = ["Yes","No","Only on Friday"] reply = choicebox("Do you like to eat fish?", choices=choices)
There are a number of functions built on top of buttonbox() for common needs. The simplest of these is
def msgbox(msg="(Your message goes here)", title="", button_text="OK"): ....
msgbox("Backup complete!",button_text="Good job!")
NOTE that these boxes return integer values (1 and 0), not true boolean values (true and false), which became available in Python version 2.3.
msgbox("Hello, world!") |
![]() |
msg = "Do you want to continue?" title = "Please Confirm" if ccbox(msg, title): # show a Continue/Cancel dialog pass # user chose Continue else: # user chose Cancel sys.exit(0) |
![]() |
To specify your own set of buttons in a buttonbox, use the buttonbox() function.
The buttonbox can be used to display a set of buttons of your choice. When the user clicks on a button, buttonbox() returns the text of the choice. If the user cancels or closes the buttonbox, the default choice (the first choice) is returned.
Here is a simple example of a boolbox():
message = "What does she say?" title = "" if boolbox(message, title, ["She loves me", "She loves me not"]): sendher("Flowers") else: pass
Buttonboxes are good for offering the user a small selection of short choices. But if there are many choices, or the text of the choices is long, then a better strategy is to present them as a list.
choicebox provides a way for a user to select from a list of choices. The choices are specified in a sequence (a tuple or a list). The choices will be given a case-insensitive sort before they are presented.
The keyboard can be used to select an element of the list.
Pressing "g" on the keyboard, for example, will jump the selection to the first element beginning with "g". Pressing "g" again, will jump the cursor to the next element beginning with "g". At the end of the elements beginning with "g", pressing "g" again will cause the selection to wrap around to the beginning of the list and jump to the first element beginning with "g".
If there is no element beginning with "g", then the last element that occurs before the position where "g" would occur is selected. If there is no element before "g", then the first element in the list is selected.
msg ="What is your favorite flavor?" title = "Ice Cream Survey" choices = ["Vanilla", "Chocolate", "Strawberry", "Rocky Road"] choice = choicebox(msg, title, choices) |
![]() Click image to enlarge it. |
# another exaple of a choicebox |
![]() Click image to enlarge it. |
The multchoicebox() function provides a way for a user to select from a list of choices. The interface looks just like the choicebox, but the user may select zero, one, or multiple choices.
The choices are specified in a sequence (a tuple or a list). The choices will be given a case-insensitive sort before they are presented.
# an example of a multchoicebox |
![]() Click image to enlarge it. |
# an example of a passwordbox |
![]() |
If there are fewer values than names, the list of values is padded with empty strings until the number of values is the same as the number of names.
If there are more values than names, the list of values is truncated so that there are as many values as names.
Returns a list of the values of the fields, or None if the user cancels the operation.
Here is some example code, that shows how values returned from multenterbox can be checked for validity before they are accepted.
msg = "Enter your personal information" title = "Credit Card Application" fieldNames = ["Name","Street Address","City","State","ZipCode"] fieldValues = [] # we start with blanks for the values fieldValues = multenterbox(msg,title, fieldNames) # make sure that none of the fields was left blank while 1: if fieldValues == None: break errmsg = "" for i in range(len(fieldNames)): if fieldValues[i].strip() == "": errmsg = errmsg + ('"%s" is a required field.\n\n' % fieldNames[i]) if errmsg == "": break # no problems found fieldValues = multenterbox(errmsg, title, fieldNames, fieldValues) print "Reply was:", fieldValues
Here is some example code, that shows how values returned from multpasswordbox can be checked for validity before they are accepted.
msg = "Enter logon information" title = "Demo of multpasswordbox" fieldNames = ["Server ID", "User ID", "Password"] fieldValues = [] # we start with blanks for the values fieldValues = multpasswordbox(msg,title, fieldNames) # make sure that none of the fields was left blank while 1: if fieldValues == None: break errmsg = "" for i in range(len(fieldNames)): if fieldValues[i].strip() == "": errmsg = errmsg + ('"%s" is a required field.\n\n' % fieldNames[i]) if errmsg == "": break # no problems found fieldValues = multpasswordbox(errmsg, title, fieldNames, fieldValues) print "Reply was:", fieldValues
easygui provides functions for displaying text.
Note that you can pass codebox() and textbox() either a string or a sequence. A sequence will be converted to text before being displayed. This means that you can use these functions to display the contents of a file this way:
filename = os.path.normcase("c:/autoexec.bat") f = open(filename, "r") text = f.readlines() f.close() codebox("Contents of file " + filename, "Show File Contents", text)
A common need is to ask the user for a filename or for a directory. easygui provides a few basic functions for allowing a user to navigate through the file system and choose a directory or a file. (These functions are wrappers around widgets and classes in lib-tk.)
Note that in the current version of easygui, the startpos argument is not supported.