tkinter case insensitive bind

↘锁芯ラ 提交于 2019-12-10 10:36:54

问题


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


回答1:


Yes, you have to make two bindings.




回答2:


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)


来源:https://stackoverflow.com/questions/7402516/tkinter-case-insensitive-bind

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