##installing the module easygui
!pip install easygui
##or
pip install easygui
First,we are going to create a simple GUI box with "hello world" in it!pip install easygui
##or
pip install easygui
Code
##import to import modules
##if error run pip install easygui import easygui ##function to create a box that has a hello world text in it easygui.msgbox("Hello World!")
Output##if error run pip install easygui import easygui ##function to create a box that has a hello world text in it easygui.msgbox("Hello World!")
Click ok to close the box
The above example does not show much but the next example will show more about the power of GUI
We’re going to use the example of choosing your favorite flavor of ice cream to look at some different ways to get input (the ice cream flavor) from the user with EasyGui.
Dialog box with multiple button
Let’s make a dialog box (like a message box) with more than one button. The way to do this is with a button box (buttonbox)
Code
Output
i Click on chocolate and it shows...
Choice box EasyGui has something called a choice box (choicebox), which presents a list of choices. The user picks one and then clicks the OK button.
To try this, we only need to make one small change to our program: change buttonbox to choicebox
Code
Output
I double click chocolate
After you click a flavor and then click OK, you’ll see the same kind of message box as before. Notice that, in addition to clicking choices with the mouse, you can select a flavor with the up and down arrow keys on the keyboard. If you click Cancel, the program will end, and you’ll also see an error. That’s because the last line of
the program is expecting some text (like Vanilla), but if you click Cancel, it doesn’t get any.
Text input
What if you want something more like input(), where the user can type in text? That way, she can enter any flavor she wants. EasyGui has something called an enter box (enterbox) to do just that.
Run the code below
Code
Output
i type peppermint and click ok
Default input
Sometimes when a user is entering information, a certain answer is expected, common, or most likely to be entered. That is called a default. You might be able to save the user some typing by automatically entering the most common answer for him. Then he’d only have to type an answer if he had a different input. To put a default in an enter box, change your program to look like the code below
Code
Output
Now, when you run it, “Vanilla” is already entered in the enter box. You can delete it and enter anything you want, but if your favorite flavor is vanilla, you don’t have to type anything: just click OK.
Numbers
What if you want to type numbers?
EasyGui also has something called an integer box (integerbox), which you can use for entering integers. You can set a lower and upper limit to the number that can be entered.
Let modify the above code
Output
In the next post,we will be trying out the number guessing game again,this time with the power of GUI
##importing the module easygui
import easygui
##a button box hold the text and the choices
flavor = easygui.buttonbox("What is your favorite ice cream flavor?",
choices = ['Vanilla', 'Chocolate', 'Strawberry'] )
##the flavor is save when you click one of the the three ice cream flavor "Vanilla","Chocolate ","Strawberry"
easygui.msgbox ("You picked " + flavor)
Output
i Click on chocolate and it shows...
Choice box EasyGui has something called a choice box (choicebox), which presents a list of choices. The user picks one and then clicks the OK button.
To try this, we only need to make one small change to our program: change buttonbox to choicebox
Code
##importing the module the easygui
import easygui
##set flavor as the choices for the choices that you click
flavor = easygui.choicebox("What is your favorite ice cream flavor?", choices = ['Vanilla', 'Chocolate','Strawberry'] )
##print out the message after you click on the flavor
easygui.msgbox ("You picked " + flavor)
import easygui
##set flavor as the choices for the choices that you click
flavor = easygui.choicebox("What is your favorite ice cream flavor?", choices = ['Vanilla', 'Chocolate','Strawberry'] )
##print out the message after you click on the flavor
easygui.msgbox ("You picked " + flavor)
Output
I double click chocolate
After you click a flavor and then click OK, you’ll see the same kind of message box as before. Notice that, in addition to clicking choices with the mouse, you can select a flavor with the up and down arrow keys on the keyboard. If you click Cancel, the program will end, and you’ll also see an error. That’s because the last line of
the program is expecting some text (like Vanilla), but if you click Cancel, it doesn’t get any.
Text input
What if you want something more like input(), where the user can type in text? That way, she can enter any flavor she wants. EasyGui has something called an enter box (enterbox) to do just that.
Run the code below
Code
##importing the module easygui
import easygui
##set flavor as whatever you want
##this is similar to input()
flavor = easygui.enterbox("What is your favorite ice cream flavor?")
##print out the message after you type inside the text box
##text you type,e.g I type peppermint
easygui.msgbox ("You entered " + flavor)
import easygui
##set flavor as whatever you want
##this is similar to input()
flavor = easygui.enterbox("What is your favorite ice cream flavor?")
##print out the message after you type inside the text box
##text you type,e.g I type peppermint
easygui.msgbox ("You entered " + flavor)
Output
i type peppermint and click ok
Default input
Sometimes when a user is entering information, a certain answer is expected, common, or most likely to be entered. That is called a default. You might be able to save the user some typing by automatically entering the most common answer for him. Then he’d only have to type an answer if he had a different input. To put a default in an enter box, change your program to look like the code below
Code
##import the module
import easygui
##set the flavor by whatever you type,default is vanilla if you did not type anything
flavor = easygui.enterbox("What is your favorite ice cream flavor?", default = 'Vanilla')
##print out the flavor that you entered or the default vanilla
easygui.msgbox ("You entered " + flavor)
import easygui
##set the flavor by whatever you type,default is vanilla if you did not type anything
flavor = easygui.enterbox("What is your favorite ice cream flavor?", default = 'Vanilla')
##print out the flavor that you entered or the default vanilla
easygui.msgbox ("You entered " + flavor)
Output
Now, when you run it, “Vanilla” is already entered in the enter box. You can delete it and enter anything you want, but if your favorite flavor is vanilla, you don’t have to type anything: just click OK.
Numbers
What if you want to type numbers?
EasyGui also has something called an integer box (integerbox), which you can use for entering integers. You can set a lower and upper limit to the number that can be entered.
Let modify the above code
##importing the module easygui
import easygui
##set the default for favouritenum to 1
##whatever you type in will be set to favouritenum
favouritenum = easygui.integerbox("What is your favorite number?", default = '1')
##print out whatever you type in or the default num
##need to change int to str,if not error
easygui.msgbox ("You entered " + str(favouritenum))
import easygui
##set the default for favouritenum to 1
##whatever you type in will be set to favouritenum
favouritenum = easygui.integerbox("What is your favorite number?", default = '1')
##print out whatever you type in or the default num
##need to change int to str,if not error
easygui.msgbox ("You entered " + str(favouritenum))
Output
As you can see,number 1 is already the default number.We are going to choose the default number and click on
In the next post,we will be trying out the number guessing game again,this time with the power of GUI













No comments:
Post a Comment