Events

Tk events can come from various sources including:

  • mouse (move, button)
  • keyboard
  • widget (enter/leave)

Each widget can bind a handler method to an event:

widget.bind(event, handler)

If an event matching the event pattern happens, the corresponing handler is called.

Write events to the status bar

Here is an exemple which prints mouse Button and Motion events to the status bar:

class Demo(App):
    def __init__(self):
        super().__init__()
        Label("Button and Motion events", font="Arial 24")
        Label('Display the event in the status bar')

        App.root.bind('<Button>', self.cb)
        App.root.bind('<Motion>', self.cb)

    def cb(self, event):
        """Callback function."""
        App.status['text'] = event

This is a screen capture of the above program.

../_images/event1.png
from tklib import *

def cb(event):
    """Callback function."""
    print(event)  

app = App('Events and bindings')

Label("Button and Motion events", font="Arial 24")
Label('Display the event in the status bar')

app.root.bind('<Button>', cb)
app.root.bind('<Motion>', cb)

app.run()

event1.py

Write events to a text widget

The following program sends Button and Mouse events to a Text widget, just by changing the callback function:

def cb(self, event):
    """Callback function."""
    App.txt.insert('end', str(event) + '\n')

This is a screen capture of the above program.

../_images/event2.png
"""Write Button and Mouse events to a Text widget."""
from tklib import *

class Demo(App):
    def __init__(self):
        super().__init__()
        Label("Button and Motion events", font="Arial 24")
        App.txt = Text(scroll='y')

        App.root.bind('<Button>', self.cb)
        App.root.bind('<Motion>', self.cb)

    def cb(self, event):
        """Callback function."""
        App.txt.insert('end', str(event) + '\n')
        
if __name__ == '__main__':
    Demo().run()

event2.py

Enter, leave and return events

The following program detects these events:

App.root.bind('<Enter>', self.cb)
App.root.bind('<Leave>', self.cb)
App.root.bind('<Return>', self.cb)
App.root.bind('<Configure>', self.cb)

This is a screen capture of the above program.

../_images/event3.png
from tklib import *

class Demo(App):
    """Write Enter, Leave and Return events to a Text widget."""
    def __init__(self):
        super().__init__()
        Label("Enter, Leave and Return events", font="Arial 24")
        
        App.txt = Text()
        App.txt.grid(sticky='nswe')

        App.root.bind('<Enter>', self.cb)
        App.root.bind('<Leave>', self.cb)
        App.root.bind('<Return>', self.cb)
        App.root.bind('<Configure>', self.cb)

    def cb(self, event):
        """Callback function."""
        App.txt.insert('end', str(event) + '\n')
        
if __name__ == '__main__':
    Demo().run()

event3.py

Keyboard events

Specific keyboard events can be bound to a specific widget and trigger a callback function. In the example below we bind different keys to the root widget in order to call a callback function. The callback function inserts the event despcriptor or a short text into a Text widget.

  • <Key> - any key
  • a - a lower-case a (or any other letter)
  • A - an upper-case A
  • <Return> - the Return key
  • <Escape> - the Escape key
  • <Tab> - the Tab key

Modifier keys can also bind to a callback function. They work equally for the left and the right keys.

  • <Shift_L> for the Shift keys
  • <Control_L> for the Control keys
  • <Alt_L> for the Alt keys
  • <Meta_L> for the Command key (on the Mac)
  • <Super_L> for the Fn key (on the Mac)

Finally we configure the BackSpace key to clear the screen:

root.bind('<BackSpace>', lambda e: text.delete('1.0', 'end'))

The x=-5, y=-50 coordinates are the position of the widget.

This is a screen capture showing the trace of various key presses.

../_images/event4.png
# keyboard bindings
import tkinter as tk

def cb(event):
    text.insert('end', str(event) + '\n')

root = tk.Tk()
text = tk.Text(root)
text.grid()

root.bind('<Key>', cb)
root.bind('a', lambda e: cb('a'))
root.bind('A', lambda e: cb('A'))
root.bind('<Return>', lambda e: cb('Return'))
root.bind('<Escape>', lambda e: cb('Escape'))
root.bind('<Tab>', lambda e: cb('Tab'))
root.bind('<space>', lambda e: cb('space'))
root.bind('<F1>', lambda e: cb('F1'))

# modifier keys (left and right)
root.bind('<Shift_L>', lambda e: cb('Shift'))
root.bind('<Control_L>', lambda e: cb('Ctrl'))
root.bind('<Alt_L>', lambda e: cb('Alt'))
root.bind('<Meta_L>', lambda e: cb('Cmd'))
root.bind('<Super_L>', lambda e: cb('Fn'))

# modifier keys for arrows
root.bind('<Right>', lambda e: cb('Right'))
root.bind('<Shift-Right>', lambda e: cb('Shift-Right'))
root.bind('<Command-Right>', lambda e: cb('Command-Right'))

# clear screen
root.bind('<BackSpace>', lambda e: text.delete('1.0', 'end'))
root.mainloop()

event4.py