So far, everything we've done doesn't have an image displayed. Weird to think, right? You always thought you were displaying images, weren't you? Well, you weren't. All the pictures you see on every site, every game, even this blog and Python IDLE, uses something that displays images called GUIs.
Now, what is a GUI? It stands for Graphical User Interface. Those are big words, so let me simplify it: images on the screen the person playing the game (or anything else) interacts with. If you don't know what "interacts" means... Oh well.
GUI is an acronym, but it's pronounced "gooey."
Now, to use a GUI in Python, you need to have a GUI program. We will start out with EasyGui. And, before you download it, let me tell you that you need to put it in a folder Python can find. It should automatically do that, but, just in case, check where the file will be, and put it in a Python file. I recommend putting the file where Python itself is, which is called Python33. Alright, now that you have been informed, the link to the EasyGui download is here.
Now, let's start with the basics of GUIs. You actually don't need to write a program to use EasyGui, so just type this:
import easygui
Saying that allows you to use EasyGui! Now, do this:
easygui.msgbox("Hi!")
A window should pop up saying "Hi!," and under that is a button that says "OK." Press that, and in Python, it will display what you pressed, which was 'OK'
Msgbox is message box, a window that has only one button to press, "OK." After you press that button, you should get this in Python IDLE:
'OK'
Python displays what you select. However, this is not the only thing you can do with GUIs. Another type of GUI window called button box.
Button boxes may be a little bit tricky, but you will get used to them. Type this:
easygui.buttonbox("Hi!")
A window should pop up saying, "Hi!" and 3 options to click: Button1, Button2, and Button3. Let's say, if you click Button2, Python will say:
'Button2'
Like the message box, Python displays your answer. However, what if we want to rename the buttons? Do this:
choices=easygui.buttonbox("Hi!", choices=["Hi!", "GO AWAY"])
Choose either button. This time, it does not display your choice. Now, to make EasyGui respond to the button you selected, try this:
if choices == "Hi!":
easygui.msgbox(":)")
elif choices == "GO AWAY":
easygui.msgbox(":(")
If you chose the first button, you should get a window that says, ":)." If you chose the other option, you should get ":(." Hopefully you remember how to use button boxes. Maybe it's just me that always forgets how to use them.
There's another way button boxes work. This one is less complicated or however you think of it.
easygui.buttonbox("Hi!",
choices=["Hi!", "GO AWAY"])
This works exactly like the other way. You can choose which way you want to do it, but I prefer the first way.
There's also a type of window called a choice box. It works like a button box, but has a different layout. Type this:
choices=easygui.choicebox("Hi!", choices=["Hi!", "GO AWAY"])
You should get a large box with a big white area. The topmost line will say "GO AWAY" and be highlighted. Under that should be "Hi!" When you select one of the choices, that line is highlighted. Nothing will happen when you click OK, but, if you want, you can do what we did with the button box and make it respond. Also, if you don't want the GUI to be so big, we will discuss that in the next article.
Yet another way to use EasyGui is with an enter box. Type this in Python:
easygui.enterbox("Hi!")
You should get a window that says "Hi!" and a white bar under it, where you can type anything you want. When you type in what you want to say and click OK, Python will display what you typed. If you want the GUI to respond to a certain thing you typed, you have to do this:
input=easygui.enterbox("Hi!")
The window will pop up. Now say something, like, "Hi!"
if input == "Hi!":
easygui.msgbox("Nice to meet you!")
An enter box isn't the best choice to do if you want a certain input, considering the trillions of things people could type, but it is good for a game where you have to figure out a code.
Finally, there's the integer box. There is no float box that I've heard of (yet), but this box allows you to type in a whole number. Type this:
easygui.integerbox("What is 1 + 1?")
You can type in whatever you want, but if it's not an integer, it will display the window again. If you want a certain response, you will have to do the same thing we did with the enter box.
In this article, we learned about the 5 kinds of GUI boxes. There's no limit to GUIs! As promised, the next article will pertain to how you can make the enter box smaller.
No comments:
Post a Comment