Multiple Key Event Bindings in Tkinter - "Control + E" "Command (apple) + E" etc Multiple Key Event Bindings in Tkinter - "Control + E" "Command (apple) + E" etc tkinter tkinter

Multiple Key Event Bindings in Tkinter - "Control + E" "Command (apple) + E" etc


With Tkinter, "Control-R" means Ctrl-Shift-R whereas "Control-r" means Ctrl-R. So make sure you're not mixing up uppercase and lowercase.


This appears to be a bug in Tk. I get the same error with tcl/tk on the mac as well as with python/tkinter. You can bind <Command-e> to a widget (I tried with a text widget) but binding it to the root window or to "all" seems to cause the error you get.


Enhanced to cover the Alt and Meta keys, aka Option and Command on macOS.

# Original <https://StackOverflow.com/questions/6378556/#           multiple-key-event-bindings-in-tkinter-control-e-command-apple-e-etc># Status of alt (ak option), control, meta (aka command)# and shift keys in Python tkinter# Note, tested only on macOS 10.13.6 with Python 3.7.4 and Tk 8.6.9import tkinter as tkimport sys_macOS = sys.platform == 'darwin'_Alt   = 'Option' if _macOS else 'Alt'_Ctrl  = 'Control'_Meta  = 'Command' if _macOS else 'Meta'_Shift = 'Shift'alt = ctrl = meta = shift = ''def up_down(mod, down):    print('<%s> %s' % (mod, 'down' if down else 'up'))    return downdef key(event):    '''Other key pressed or released'''    # print(event.keycode, event.keysym, event.down)    global alt, ctrl, meta, shift    t = [m for m in (alt, ctrl, shift, meta, str(event.keysym)) if m]    print('+'.join(t))def alt_key(down, *unused):    '''Alt (aka Option on macOS) key is pressed or released'''    global alt    alt = up_down(_Alt, down)def control_key(down, *unused):    '''Control key is pressed or released'''    global ctrl    ctrl = up_down(_Ctrl, down)def meta_key(down, *unused):    '''Meta (aka Command on macOS) key is pressed or released'''    global meta    meta = up_down(_Meta, down)def shift_key(down, *unused):    '''Shift button is pressed or released'''    global shift    shift = up_down(_Shift, down)def modifier(root, mod, handler, down):    '''Add events and handlers for key press and release'''    root.event_add('<<%sOn>>' % (mod,), ' <KeyPress-%s_L>' % (mod,), '<KeyPress-%s_R>' % (mod,))    root.bind(     '<<%sOn>>' % (mod,), lambda _: handler('<%s>' % (down,)))    root.event_add('<<%sOff>>' % (mod,), '<KeyRelease-%s_L>' % (mod,), '<KeyRelease-%s_R>' % (mod,))    root.bind(     '<<%sOff>>' % (mod,), lambda _: handler(''))root = tk.Tk()root.geometry('256x64+0+0')modifier(root, 'Alt',     alt_key,     _Alt)modifier(root, 'Control', control_key, _Ctrl)modifier(root, 'Meta',    meta_key,    _Meta)modifier(root, 'Shift',   shift_key,   _Shift)root.bind('<Key>', key)root.mainloop()