Mouse Events
Events can come from various sources, including key presses and mouse operations by the user.
Tkinter provides a powerful mechanism to let you deal with events yourself. For each widget, you can bind Python functions and methods to events.widget.bind(event, handler)
If an event matching the event description occurs in the widget, the given handler is called with an object describing the event.
Tkinter provides a powerful mechanism to let you deal with events yourself. For each widget, you can bind Python functions and methods to events.widget.bind(event, handler)
If an event matching the event description occurs in the widget, the given handler is called with an object describing the event.
from tkinter import * def buttonPressed( event ): var.set( "Pressed at [ " + str( event.x ) + ", " + str( event.y ) + " ]" ) def buttonReleased( event ): var.set( "Released at [ " + str( event.x ) + ", " + str( event.y ) + " ]" ) def enteredWindow( event ): var.set( "Mouse in window" ) def exitedWindow( event ): var.set( "Mouse outside window" ) def mouseDragged( event ): var.set( "Dragged at [ " + str( event.x ) + ", " + str( event.y ) + " ]" ) base = Tk() base.title("Mouse Events") frame = Frame(base, width=300, height=300) frame.pack() var = StringVar() var.set("Mouse Events will show here") label = Label(base, textvariable=var) label.pack(side=BOTTOM) frame.bind( "<Button-1>", buttonPressed ) frame.bind( "<ButtonRelease-1>", buttonReleased ) frame.bind( "<Enter>", enteredWindow ) frame.bind( "<Leave>", exitedWindow ) frame.bind( "<B1-Motion>", mouseDragged ) base.mainloop()
Keyboard Events
With the keyboard events, the component keyboard is bound to needs to stay in focus, use widget.focus_set()
from tkinter import * def keyPressed( event ): var.set( repr(event.char) + " pressed") frame.focus_set() base = Tk() base.title("Keyboard Events") frame = Frame(base, width=300, height=300) var = StringVar() var.set("Keyboard Events will show here") label = Label(base, textvariable=var) label.pack(side=BOTTOM) frame.bind("<Key>", keyPressed) frame.focus_set() frame.pack() base.mainloop()
Event Descriptions (http://www.python-course.eu/tkinter_events_binds.php)
Event | Description |
---|---|
<Button> | A mouse button is pressed with the mouse pointer over the widget.
The detail part specifies which button, e.g.
The left mouse button is defined by the event <Button-1>, the middle button
by <Button-2>, and the rightmost mouse button by <Button-3>.
<Button-4> defines the scroll up event on mice with wheel support and and <Button-5> the scroll down. If you press down a mouse button over a widget and keep it pressed, Tkinter will automatically "grab" the mouse pointer. Further mouse events like Motion and Release events will be sent to the current widget, even if the mouse is moved outside the current widget. The current position, relative to the widget, of the mouse pointer is provided in the x and y members of the event object passed to the callback. You can use ButtonPress instead of Button, or even leave it out completely: |
<Motion> | The mouse is moved with a mouse button being held down. To specify the left, middle or right mouse button use <B1-Motion>, <B2-Motion> and <B3-Motion> respectively. The current position of the mouse pointer is provided in the x and y members of the event object passed to the callback, i.e. event.x, event.y |
<ButtonRelease> | Event, if a button is released. To specify the left, middle or right mouse button use <ButtonRelease-1>, <ButtonRelease-2>, and <ButtonRelease-3> respectively. The current position of the mouse pointer is provided in the x and y members of the event object passed to the callback, i.e. event.x, event.y |
<Double-Button> | Similar to the Button event, see above, but the button is
doubble clicked instead of a single click.
To specify the left, middle or right mouse button use <Double-Button-1>,
<Double-Button-2>, and
<Double-Button-3> respectively. You can use Double or Triple as prefixes. Note that if you bind to both a single click (<Button-1>) and a double click (<Double-Button-1>), both bindings will be called. |
<Enter> | The mouse pointer entered the widget. Attention: This doesn't mean that the user pressed the Enter key!. <Return> is used for this purpose. |
<Leave> | The mouse pointer left the widget. |
<FocusIn> | Keyboard focus was moved to this widget, or to a child of this widget. |
<FocusOut> | Keyboard focus was moved from this widget to another widget. |
<Return> | The user pressed the Enter key. You can bind to virtually all keys on the keyboard: The special keys are Cancel (the Break key), BackSpace, Tab, Return(the Enter key), Shift_L (any Shift key), Control_L (any Control key), Alt_L (any Alt key), Pause, Caps_Lock, Escape, Prior (Page Up), Next (Page Down), End, Home, Left, Up, Right, Down, Print, Insert, Delete, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, Num_Lock, and Scroll_Lock. |
<Key> | The user pressed any key. The key is provided in the char member of the event object passed to the callback (this is an empty string for special keys). |
a | The user typed an "a" key. Most printable characters can be used as is. The exceptions are space (<space>) and less than (<less>). Note that 1 is a keyboard binding, while <1> is a button binding. |
<Shift-Up> | The user pressed the Up arrow, while holding the Shift key pressed. You can use prefixes like Alt, Shift, and Control. |
<Configure> | The size of the widget changed. The new size is provided in the width and height attributes of the event object passed to the callback. On some platforms, it can mean that the location changed. |