Previous

A game with input

Next

Computer languages all do the same things. They just do them differently. But all languages will do these things:

  1. Display messages for a user to read.
  2. Accept information from a user.
  3. Store information.
  4. Do arithmetic to calculate a value.
  5. Compare one value to some other value.
  6. Do something based on the comparison.

These are all pretty simple things. Like computers, computer programs are built out of small simple parts that get more and more complex and clever as you put them together.

We've seen a simple way to do number 1, displaying a message to the user, so lets do number 2 now.

There are a lot of ways to get information from a user.

In the old days, the user would put data on a card with a lot of special holes in it and the computer would convert the patterns of holes into data values.

Nowadays, computer programs use what's called a GUI - that stands for Graphical User Interface. That means all the buttons, screen backgrounds, mice, and stuff that we're used to. Some folks call this a WIMP interface - that stands for Windows Icons Mouse Pointer.

So, to get some input from a user, lets write a GUI for our game.

In Tcl/Tk, we call the GUI images that show up on your computer screen widgets or windows. There are about a dozen different widgets in Tcl/Tk. The widgets include the tk_messageBox we just looked at, as well as buttons, labels, scrollbars and stuff like that.

One that gets used a lot is the button widget. This is a simple widget that shows some text (or an image), and you can click on it with your mouse. When you click a button, it will do something.

This game is almost as simple as the last one. It will have two buttons, so you can decide whether you want to win or lose. It looks like this:

The tk_messageBox needed to know what buttons to show and what message to display. The button command requires different pieces of information. It needs to know

There are two buttons in this game. We create the buttons with the Tcl/Tk button command like this:


button .win -text "Win" \
    -command {tk_messageBox -type ok -message "You Win"}

The .win is the name of this button. All widget names have to start with a period followed by a lower case letter. After that, you can use anything except a space. The button name has to be the first word after the command button.

The -text is an argument that tells Tcl that the next thing it sees is the text to display in the button.

The -command is an argument that tells Tcl that the next thing it sees is the Tcl/Tk command to run when someone clicks the button.

After the wish interpreter has created a button, the computer knows that the button exists, but it doesn't know where to put it. You tell the computer where to display the widget with the grid command.

For a simple game like this, you can just type grid and a list of widgets to display.

When you put several widgets after the grid command, it puts them on a single line, arranged from left to right in the order you typed them in.

Each time there is a new grid command, it will put them on a new line. The widgets are always placed in columns so that they line up with the line above them.

Here's the Tcl/Tk commands to make this game:


button .win -text "Win" -command {tk_messageBox -type ok -message "You Win"}
button .lose -text "Lose" -command {tk_messageBox -type ok -message "You Lose"}
grid .win .lose

The first command creates a button named .win. The button will have the text Win in it. When you click that button, it will open a tk_messageBox to tell you that you've won.

The next line does the same thing to create a button named .lose, with Lose in the button and a command to put up a tk_messageBox that says you've lost.

The last line tells the grid command to display these two buttons with the .win button on the left of the .lose button.

Here's a few rules about the Tcl/Tk language.

There are a few more rules, and we'll get to them later.

We can make a slightly more interesting number guessing game by changing the buttons to show a 1 or a 2 instead of Win and Lose and then adding some labels to tell the user what to do.

The new game will look like this.

The label command looks a lot like the button command, except that it doesn't have a -command option.

The definition for the label looks like this:


Syntax: label .labelName -option value
Creates a label named .labelName
-text Display the following string as text in the label.

You can use a label the same way you would use a button. For instance, the label might say you wrote the program:


label .author -text "This marvelous game written by ME"
grid .author

The code below will make this game.


label .info1 -text "I've got a secret number."
label .info2 -text "Can you guess it?"
grid .info1 .info2
button .win -text "1" -command {tk_messageBox -type ok -message "You Win"}
button .lose -text "2" -command {tk_messageBox -type ok -message "You Lose"}
grid .win .lose


Type (or copy/paste) this game in Komodo and play with it a little bit. Try adding some more numbers and changing the "You Win" number.
We introduced a few new things in this lesson. The important parts are:

Again, this game is pretty simplistic. Once you know the secret number is 1, you'll never need to lose the game.

In the next lesson, the game will decide what the winning number is. That will make it a bit harder to win.



Previous

Next


Copyright 2007 Clif Flynt