Web Analytics Made Easy - Statcounter
Skip to content

Binding Tkinter "events"

If you wish to receive events directly from tkinter, but do it in a PySimpleGUI way, then you can do that and get those events returned to you via your standard Window.read() call.

Both the Elements and Window objects have a method called bind. You specify 2 parameters to this function. One is the string that is used to tell tkinter what events to bind. The other is a "key modifier" for Elements and a "key" for Windows.

The key_modifier in the Element.bind call is something that is added to your key. If your key is a string, then this modifier will be appended to your key and the event will be a single string.

If your element's key is not a string, then a tuple will be returned as the event (your_key, key_modifier)

This will enable you to continue to use your weird, non-string keys. Just be aware that you'll be getting back a tuple instead of your key in these situations.

The best example of when this can happen is in a Minesweeper game where each button is already a tuple of the (x,y) position of the button. Normal left clicks will return (x,y). A right click that was generated as a result of bind call will be ((x,y), key_modifier).

It'll be tricky for the user to parse these events, but it's assumed you're an advanced user if you're using this capability and are also using non-string keys.

An Element member variable user_bind_event will contain information that tkinter passed back along with the event. It's not required for most operations and none of the demos currently use this variable, but it's there just in case. The contents of the variable are tkinter specific and set by tkinter so you'll be digging into the tkinter docs if you're using an obscure binding of some kind.

tkinter events must be in between angle brackets

window['-KEY-'].bind('<TKINTER EVENT>', 'STRING TO APPEND')

Events can also be binded to the window

window.bind('<TKINTER EVENT>', 'STRING TO APPEND')

List of tkinter events:

Event Description
Button-1 / ButtonPress-1 / 1 Left button is pressed over an element. 1 corresponds to the left button, 2 to the middle button, 3 to the right button.
Buttons can go up to 5
ButtonRelease-1 Left button is released over an element.
Double-Button-1 An element was double clicked. The 'Double' modifier was used. See below for more modifiers.
B1-Motion Left button is held and moved around over an element.
Motion Mouse pointer is moved over an element
Enter Mouse pointer entered the element
Leave Mouse pointer left the element
Key / KeyPress
Keypress-a / a
A key was pressed. Keysyms can be used to bind specific key/s.
When using keysyms, 'Key' or 'KeyPress' can be omitted.
KeyReleased A key was released.
FocusIn Keyboard has focused on element.
FocusOut Keyboard switched focus from element.
Visibility Some part of the element is seen on screen

Modifier keys can be put in front of events.

Windows MacOS
Control Command
Alt Option
Shift <==
Double <==
Triple <==
Quadruple <==

The following will bind Ctrl+z to the window:

window.bind('<Control-z>', 'STRING TO APPEND')

To unbind an event from an element, use the unbind method.

window['-KEY-'].unbind('TKINTER EVENT')

Here is sample code that shows these bindings in action.

Four main things are occurring.

  1. Any button clicks in the window will return an event "Window Click" from window.read()
  2. Right clicking the "Go" buttons will return an event "Go +RIGHT CLICK+" from window.read()
  3. When the second Input Element receives focus, an event "-IN2- +FOCUS+" will be returned from window.read()
  4. If the "Unbind " button is pressed, the right click binding of the "Go" button will be unbinded.
import PySimpleGUI as sg

sg.theme('Dark Green 2')

layout = [  [sg.Text('My Window')],
            [sg.Input(key='-IN1-')],
            [sg.Input(key='-IN2-')],
            [sg.Button('Go'), sg.Button('Unbind'),sg.Button('Exit')]
              ]

window = sg.Window('Window Title', layout, finalize=True)

window.bind("<Button-1>", 'Window Click')
window['Go'].bind("<Button-3>", ' +RIGHT CLICK+')
window['-IN2-'].bind("<FocusIn>", ' +FOCUS+')

while True:             # Event Loop
    event, values = window.read()
    print(event, values)
    if event in (sg.WIN_CLOSED, 'Exit'):
        break
    if event == 'Unbind':
        window['Go'].unbind('<Button-3>')

window.close()

Tkinter bindings documentation