tkinter case insensitive bind

故事扮演 提交于 2019-12-06 10:49:49

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!