See All Titles |
![]() ![]() Handling Tkinter EventsUsually, when you create a graphical interface for your application, you want to handle all the possible events that happen there, such as reading in each key in the keyboard (including the F1F12 set, Ctrl, Alt, and Shift keys), tracking the actions upon the mouse button, or even controlling the window redraw events fired by the window manager. Tkinter handles that by allowing you to create bindings for every specific object. Actually, you can bind events to the widget instance itself, to the widget's Toplevel window, to the widget's class, and to your entire application (such as a global HELP functionality for the F1 function key). After binding an event to a widget, you need to specify which function should be called at the time the event occurs. This function (or method) is called a callback. You can define callbacks for all kinds of windowing events, as you will see later. The following code demonstrates a simple callback functionality, which is associated to the command property from a specific widget. from Tkinter import * import sys def close(): sys.exit(0) root = Tk() button = Button(root) button['text'] = "Close" button['command'] = close button.pack() root.mainloop() The next example binds the mouse-click event ("<Button-1>") to a specific function in our program. Note that the event description is just a simple string. The mainloop keeps checking for this event, and when it catches the event, the function (event handler) is called. Note that an object is passed to the callback function carrying some information provided by the event. from Tkinter import * def ShowPosition(event): Top = Toplevel(root) xlabel = Label(Top) xlabel.pack() xlabel.config(text = "X = " + str(event.x)) ylabel = Label(Top) ylabel.pack() ylabel.config(text = "Y = " + str(event.y)) Top.mainloop() root = Tk() frame = Frame(root, width=200, height=200) frame.bind("<Button-1>", ShowPosition) frame.pack() root.mainloop() The next sections provided more events that you can use in your programs. Mouse EventsWhen handling mouse event, use 1 for the left button, 2 for the middle button, and 3 for the right button. The following events are based on the left button, and you need to make the necessary changes in order to adapt them for usage with the other buttons. Before starting, you should know that the current position of the mouse pointer, the position relative to the widget, is provided in the x and y options of the event object passed to the callback. If you bind to both a single click event and to a double click event, both bindings will be called whenever one of them is activated.
Keyboard EventsThe following events are exposed by the keyboard interface:
The same concept can be applied for all the other printable characters.
The same concept can also be applied for all the other special keys found in the keyboard, including: F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, Num_Lock, Scroll_Lock, Caps_Lock, Print, Insert, Delete, Pause, Prior (Page Up), Next (Page Down), BackSpace, Tab, Cancel (Break), Control_L (any Control key), Alt_L (any Alt key), Shift_L (any Shift key), End, Home, Up, Down, Left, and Right. Event AttributesNext, I list all the attributes that are exposed by the instance objects originated by the callback functions:
Event CallbacksThe following methods are used to handle event callbacks by binding a Python function or method to an action that can be applied to a widget. You will also find some callback methods that handle alarm callbacks as well.
ProtocolsThe mechanism called protocol handler is responsible for the communication between the window manager and your application. Handling these protocols, you can intercept the messages provided by the system, and define exactly what you want to happen. Usually, the protocols WM_DELETE_WINDOW, WM_TAKE_FOCUS, and WM_SAVE_YOURSELF are the ones mostly used. For details about the other supported protocols, see the Inter-Client Communication Conventions Manual (ICCCM) at http://tronche.com/gui/x/icccm/ Although this convention was established for the X systems, the Tk library handles it on all platforms. These protocols are X specific. If you are running an X server on Windows or MacOS and have Tk compiled for X, it will do the same as on UNIX. That's because Tk ports map these calls to the equivalent actions on the other systems. The necessary syntax to bind a handler to a protocol is widget.protocol(protocol, function_handler) Note that the widget must be a Toplevel widget. The following example demonstrates the use of the WM_DELETE_WINDOW protocol. The window manager generates this protocol when the user tries to close a window. Here, we are intercepting this protocol. from Tkinter import * import tkMessageBox def protocolhandler(): if tkMessageBox.askokcancel("Exit", "Wanna leave?"): if tkMessageBox.askokcancel("Exit", "Are you sure?"): if tkMessageBox.askokcancel("Exit", "Really?"): root.destroy() root = Tk() root.protocol("WM_DELETE_WINDOW", protocolhandler) root.mainloop() Just to let you know, the WM_SAVE_YOURSELF protocol is called by X window managers when the application should save a snapshot of its working set, and the WM_TAKE_FOCUS protocol is called by X window managers when the application gets the focus.
|
© 2002, O'Reilly & Associates, Inc. |