Page 1 of 1

easygui: Make Graphical Programs with Python

#1 atik97   User is offline

  • ???
  • member icon

Reputation: 144
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Posted 07 April 2009 - 12:30 PM

easygui: Make Graphical Programs with Python

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”

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:
	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"
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"
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))
msgbox("The result is %s"%c)

So it seems so easy, isn’t it?

Is This A Good Question/Topic? 4
  • +

Replies To: easygui: Make Graphical Programs with Python

#2 OhScee   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-May 12

Posted 12 May 2012 - 10:52 AM

Super easy~
Thanks for the tutorial! :)
Was This Post Helpful? 0
  • +
  • -

#3 Miikalsen   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 85
  • Joined: 29-December 11

Posted 09 August 2012 - 12:30 PM

Is this 2.7 or 3.x ?

If its 2.7, Could you remake it into 3.x?
Was This Post Helpful? 0
  • +
  • -

#4 799arman   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 11
  • Joined: 15-May 13

Posted 15 May 2013 - 05:28 AM

Thanks for this tutorial.

Also i found the - EasyGUI Home Page

Works for both python 2.x,3.x
Was This Post Helpful? 0
  • +
  • -

#5 ConciselyVerbose   User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 315
  • Joined: 05-July 13

Posted 05 July 2013 - 10:56 PM

This is nice and well written. I do have a minor annoyance that

if choice==1:

can just be

if choice:

But everything else about this is great and very useful.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1