开发者

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 sol开发者_如何学编程ve this?

Thanks for help :)


Yes, you have to make two bindings.


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜