Command Buttons

Command Buttons

Command buttons give a user direct access to commands in your application. We've already seen how to associate a command with a button:

button .b -command someCommand

Or, if you want to associate a new command with an existing button, you can use the configure command:

.b configure -command aNewCommand

All of this is pretty simple.

Our first example simply puts the current time in a label every time you click the button. This demonstrates how commands get executed when you click on a command button:

Another simple example: start and stop some activity in your application. In this case, we have a label with a counter, and three buttons that stop and start the counter, and reset it to zero:

The above example uses a frame widget to contain the three buttons, to make them line up nicely in a row.

Actually, we only need one button, and we can cycle through the various button actions by clicking on it repeatedly:

This example shows how we can on-the-fly change the command being executed by the widget.

The next example demonstrates passing values to the command at creation time, and using current values of variables when the command is invoked. This example shows a running multiplier stored in a global value; the multiplier starts at 1, and each time a button is pressed, the current value is multiplied by the value of the label of that button.

Next, we'll check out some of the options for buttons.