SoFunction
Updated on 2024-11-16

Detailed python tkinter tutorial - event binding

A Tkinter runs mainly in the mainloop process. events can come from several places, such as keystrokes, the mouse, or system events.

Tkinter provides a rich set of methods to handle these events. For each control widget, you can bind the method function to it.

(event,handler)

If the corresponding event occurs, the handler is called to handle the event. An example:

Captures mouse click events:

from Tkinter import *

root = Tk()
def callback(event):
  print "clicked at", , 
frame = Frame(root, width=100, height=100)
("<Button-1>", callback)
()

()

Here, we use the frame's bind method to bind a callback method to an event, i.e., clicking the left mouse button, and for each click, the console prints the coordinates x, y of the current click.

Keyboard events are sent to the widget that currently has the focus, and you can use the focus_set method to set the focus of the widgets:

Captures keyboard events:

from Tkinter import *

root = Tk()

def key(event):
  print "pressed", repr()

def callback(event):
  print "clicked at", , 

frame = Frame(root, width=100, height=100)
("<Key>", key)
("<Button-1>", callback)
()

()

Run this program and you'll see that it only captures and prints when you press a keyboard key if the current window gets focus.

event

Events are defined in strings and have a special syntax rule:

<modifier-type-detail>

The type field is the most important, it indicates the type of event, which can be specified as Button, Key or Enter, Configure, etc. The modifier and detail fields provide some additional information and in most cases can be left out. There are also many ways to simplify the event string, for example, to match a keyboard key, you can omit the angle brackets and just use the key. Unless it's a space, or is itself in angle brackets.

Let's take a look at the most commonly used event formats:

Event Format:

<Button-1>

A mouse click event. 1 for the left button, 2 for the center button, and 3 for the right button. When you click a mouse button on a widget, tkinter automatically captures and triggers the event, note that the handler is executed when the button is lifted. the mouse position (relative to the widget) x, y is sent to the event object to be passed into the handler. you can also do this :, <1>, they are equivalent. I prefer this way.

<B1-Motion>

Mouse drag events. 1 for pressing the left button to drag, 2 for the middle button, and 3 for the right button. Again, the x and y of the mouse are sent to the handler as event objects.

<ButtonRelease-1>

Release after mouse press

<Double-Button-1>

double-click on a mouse

<Enter>

Note that this is the mouse pointer going into the widget, and does not represent pressing the Enter key on the keyboard.

<Leave>

Corresponding to the entry above, the mouse leaves the widget.

<FocusIn> <FocusOut>

<Return> <Cancel> <BackSpace> <Tab> <Shift_L> <Control_L>
<Alt_L> <Home> <Left> <Up> <Right> <Down> <Delete> <F1> <F2>

These keys all correspond to those on the keyboard.

<Key>

For a random keystroke, the key value is put into the event object in char format.

a b c ... 1 2 ...

Corresponds to the keys on the keyboard

<Configure>

This is key, if the size of the widget changes, or the position, the new size (width and height) will be packaged into an event and sent to the handler.

event object

Event objects are independent python instances with many properties.

Object Properties:

  1. widget generates an instance of event, not a name, and all objects have a
  2. x, y Mouse position in pixels
  3. x_root, y_root Position of the mouse relative to the upper left corner of the screen, pixels
  4. char Keyboard events only, string
  5. num Button num, mouse events only
  6. width, height widget new size
  7. type Event type

Instance Binding and Class Binding

The bind method we used above is bound to an instance object, which means that if a new instance is created, it is not bound to events.
In fact, tkinter allows you to bind events to classes and even to the program itself. You can create four levels of bindings:

  1. Bind to widget instance using bind method
  2. Binds to the widget's toplevel windows, the top-level windows, also using bind
  3. Bind to widget class using bind_class

For example, you can use bind_all to create a binding for the F1 key so you can open help anywhere.

The above is a small introduction to the python tkinter event binding detailed integration, I hope to help you, if you have any questions please leave me a message, I will promptly reply to you. I would also like to thank you very much for your support of my website!