There are several libraries in python to allow you to give your program graphical interface. Tkinter is one of them, actually it is one of the oldest GUI toolkits. easygui is a Tkinter based module which has written to give access to the GUI interactions by simple function calls. For this reason, it is so easy to use it, soon you will see that.
A simple ‘Hello World’ program
Now you will see how to write a simple program with easygui. First look at the code below, then i will explain the codes.
from easygui import * msg=”Hello World!” title=”Sample Program” msgbox(msg,title)
As you see, first we have imported the module. The next line is the message which will be shown to the window and then we have set a title for the window. The last line we have used the function msgbox(), which returns a simple window showing a message, a title and a ‘Ok’ button. You can also change the label of the button. That case you need to add another argument, ok_button=”New Label” to the msgbox() function. So you see that this function can take three arguments. The first one must be supplied, others are optional. Like this function, most of the functions take three arguments.
Some of the function calls
I have mentioned earlier here in easygui all the GUI interactions are invoked by simple function call. Now we will see some of the commonly used functions used in easygui.
We have already seen this function. What it does is simply put some message on the window.
This is a pre-defined button box which offers two buttons Yes and No. When you need to choice an options from two alternatives, then you can use it. When someone hits the yes button, it returns 1 and 0 is returned for no. Take a look at the example below-
from easygui import * msg = "Want to exit?" title = "Confirmation" choice=ynbox(msg, title) if choice==1: exit(0) else: msgbox("You have chosen to continue")
This is similar to the previous one. The only difference is instead of yes-no buttons it provides continue-cancel buttons and returns 1 for continue and 0 for cancel.
boolbox() is a user-defined button box which have two user-defined buttons along with a message and a title. When first button is chosen, it returns 1, else it returns 0.
from easygui import * msg = "What you prefer?" title = "My Chioce" choices=['Sports','Movie'] choice=boolbox(msg, title,choices) if choice==1: msgbox("You prefer Sports") elif choice==0: msgbox("You prefer Movie")
When you need to choice an option from a set of alternatives, you can use buttonbox(). This is a user-defined button set. When an option is selected, it returns the text of that particular button.
from easygui import * msg = "What is your favorite sports" title = "Favorite Sports" choices=['Cricket','Football','Golf','Other'] choice=buttonbox(msg, title,choices) if choice=='Cricket': msgbox("I like Cricket") elif choice=='Football': msgbox("I like Football") elif choice=='Golf': msgbox("I like Golg") elif choice=='Other': msgbox("None of the listed sports is my favorite one")
indexbox() is an alternative to buttonbox(). This two are very identical except here returns 0 for first choice, 1 for second choice and so on. Say, if there are there choices A, B and C, then when a user hits the choice B, it will return 1.
buttonbox() and indexbox() are ok when there are few options. But if you have a number of choices (say 10 or 20) or if you the text of the choices are long, then there comes a solution choicebox. Here all the choices are shown in the form of a list.
enterbox(), passwordbox() and integerbox()
These are used to take input from the users. The first one can take string values, the second one is used to take password that is the text is masked here and the last one is used to take integer values. So what will you do when you need to take a floating point value? No warray, you can still get it with enterbox-
from easygui import * title = "Float" a=float(enterbox("Number 1",title)) b=float(enterbox("Number 2",title)) c=a+b msgbox("The result is %s"%c)
So it seems so easy, isn’t it?