Robin Manoli
Robin Manoli

Reputation: 2222

tkinter case insensitive bind

I've noticed that when you bind keys in tkinter (python3.2 winxp), the binds are case sensitive with the letter characters. In other words binding <Control-o> does not work if you press Control+o if caps lock is active. Does it mean I need to write two bindings for each case insensitive key combination with letter characters? Or is there any way to solve this?

Thanks for help :)

Upvotes: 6

Views: 1926

Answers (2)

Walle Cyril
Walle Cyril

Reputation: 3247

You must bind twice in your case but you don't need to think about it once you write a clever function. Let's define a function that does this for us.

def bind_(widget, all_=False, modifier="", letter="", callback=None, add='',):
    if modifier and letter:
        letter = "-" + letter
    if all_:
        widget.bind_all('<{}{}>'.format(modifier,letter.upper()), callback, add)
        widget.bind_all('<{}{}>'.format(modifier,letter.lower()), callback, add)
    else:
        widget.bind('<{}{}>'.format(modifier,letter.upper()), callback, add)
        widget.bind('<{}{}>'.format(modifier,letter.lower()), callback, add)

And then use it like this:

bind_(text_widget, modifier="Control", letter="s", callback=save)
bind_(text_widget, modifier="Control-Shift", letter="s", callback=save_as)
bind_(text_widget, modifier="", letter="r", callback=print_something)

Upvotes: 2

Bryan Oakley
Bryan Oakley

Reputation: 385960

Yes, you have to make two bindings.

Upvotes: 7

Related Questions