Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions

  




 

 

Ruby Programming
Previous Page Home Next Page

Binding Events

Our widgets are exposed to the real world; they get clicked on, the mouse moves over them, the user tabs into them; all these things, and more, generate events that we can capture. You can create a binding from an event on a particular widget to a block of code, using the widget's bind method.

For instance, suppose we've created a button widget that displays an image. We'd like the image to change when the user's mouse is over the button.

image1 = TkPhotoImage.new { file "img1.gif" }
image2 = TkPhotoImage.new { file "img2.gif" }

b = TkButton.new(@root) {   image    image1   command  proc { doit } }

b.bind("Enter") { b.configure('image'=>image2) } b.bind("Leave") { b.configure('image'=>image1) }

First, we create two GIF image objects from files on disk, using TkPhotoImage. Next we create a button (very cleverly named ``b''), which displays the image image1. We then bind the ``Enter'' event so that it dynamically changes the image displayed by the button to image2, and the ``Leave'' event to revert back to image1.

This example shows the simple events ``Enter'' and ``Leave.'' But the named event given as an argument to bind can be composed of several substrings, separated with dashes, in the order modifier-modifier-type-detail. Modifiers are listed in the Tk reference and include Button1, Control, Alt, Shift, and so on. Type is the name of the event (taken from the X11 naming conventions) and includes events such as ButtonPress, KeyPress, and Expose. Detail is either a number from 1 to 5 for buttons or a keysym for keyboard input. For instance, a binding that will trigger on mouse release of button 1 while the control key is pressed could be specified as:

Control-Button1-ButtonRelease
or
Control-ButtonRelease-1

The event itself can contain certain fields such as the time of the event and the x and y positions. bind can pass these items to the callback, using event field codes. These are used like printf specifications. For instance, to get the x and y coordinates on a mouse move, you'd specify the call to bind with three parameters. The second parameter is the Proc for the callback, and the third parameter is the event field string.

canvas.bind("Motion", proc{|x, y| do_motion (x, y)}, "%x %y")
Ruby Programming
Previous Page Home Next Page

 
 
  Published under the terms of the Open Publication License Design by Interspire