3.1. The Button Widget

Of all of the widgets available with Perl/Tk, the button is one of the most commonly used. Just see all the examples in Chapter 2. When the button is pressed, something happens. That something can vary from exiting the program (as in our Hello World example) to beginning a longer series of operations such as opening a file or starting another process. The button typically displays a short text string: Done, Apply, Save, Ok, Exit.

Other widgets are also classified as buttons: radiobuttons, checkbuttons, and menubuttons. This chapter covers the traditional button. Chapter 4, and Chapter 11, will cover the other types because they look different on the screen and behave differently as well.

We cover the button widget first because it is easy to see the way different options affect it on the screen. Many of the other widgets included in Perl/Tk utilize the same options. Usually, if the option name is the same, the change to the widget will also be the same (more or less).

3.1.1. Creating a Button

The basic usage to create a button is as follows:

$button = $parentwidget->Button( [ option => value, . . . ] );

We have already seen examples of buttons in our Hello World program in Chapter 1, and in all the geometry management examples in Chapter 2. In these examples, a button is created and placed on the screen with a command like this:

$mw->Button(-text => "Done",-command ...

Get Learning Perl/Tk now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.