[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

Other bind Events


Besides those illustrated in this example, a tkinter script can register to catch additional
kinds of bindable events. For example:




  • fires when a button is released ( is run when the
    button first goes down).


  • is triggered when a mouse pointer is moved.


  • and handlers intercept mouse entry and exit in a window’s display
    area (useful for automatically highlighting a widget).


  • is invoked when the window is resized, repositioned, and so on (e.g.,
    the event object’s width and height give the new window size). We’ll make use of
    this to resize the display on window resizes in the PyClock example of Chapter 11.


  • is invoked when the window widget is destroyed (and differs from the
    protocol mechanism for window manager close button presses). Since this inter-
    acts with widget quit and destroy methods, I’ll say more about the event later in
    this section.


  • and are run as the widget gains and loses focus.


  • and are run when a window is opened and iconified.


  • , , and catch other special key presses.


  • , , and catch other arrow key presses.


This is not a complete list, and event names can be written with a somewhat sophisti-
cated syntax of their own. For instance:



  • Modifiers can be added to event identifiers to make them even more specific; for
    instance, means moving the mouse with the left button pressed, and refers to pressing the “a” key only.

  • Synonyms can be used for some common event names; for instance, <Button
    Press-1>, , and <1> mean a left mouse button press, and
    and mean the “a” key. All forms are case sensitive: use , not.

  • Virtual event identifiers can be defined within double bracket pairs (e.g., <<Paste
    Text>>) to refer to a selection of one or more event sequences.


In the interest of space, though, we’ll defer to other Tk and tkinter reference sources
for an exhaustive list of details on this front. Alternatively, changing some of the settings
in the example script and rerunning can help clarify some event behavior, too; this is
Python, after all.


More on events and the quit and destroy methods


Before we move on, one event merits a few extra words: the event (whose
name is case significant) is run when a widget is being destroyed, as a result of both


Binding Events | 447
Free download pdf