Events

The widgets in the Tk collection do a pretty good job of handling their own events. For example, the Button widget handles a mouse click by calling the function specified by –command. The Text Entry widget handles keypress events by putting the characters typed in the entry.

But sometimes you may want to handle additional events. The bind method is used to connect an event, widget, and subroutine together. For example, suppose that you want to implement a status line in which context help is displayed when the mouse is placed over a widget.

The two events you are interested are <Enter> (mouse enters the widget’s window) and <Leave> (mouse leaves the widget). The bind calls look like the following:

$tk_exit_button–>bind('<Enter>', 
    sub {display_help("Exits the program");}); 
$tk_exit_button–>bind('<Leave>', 
    sub {display_help("");});

In this case, the widget is an Exit button called $tk_exit_button. When the mouse enters the button, a help message "Exits the program" is displayed. When the mouse leaves, the message is erased.

A wide variety of events can be bound. The online documentation Tk::Bind contains a complete list.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset