多个关键事件绑定中的Tkinter - "控制+ E" "命令(苹果)+ E"等等 [英] Multiple Key Event Bindings in Tkinter - "Control + E" "Command (apple) + E" etc

查看:364
本文介绍了多个关键事件绑定中的Tkinter - "控制+ E" "命令(苹果)+ E"等等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的Mac OS X 10.6.6 - Tkinter的

我要绑定多重要事件,而我发现了一个effbot文章和Tk的手册页,我已经无法正常完成这项工作。我是新来的。

我已经成败参半。我已经能够得到按住Shift +字母键,但并不能够控制或者命令(苹果键)。我真正想要做的是命令+字母和控制+字母键所以它会在Windows和OS X理论上的工作。

我希望它在窗口层面的工作,所以我用根。或许有更好的办法。下面是我已经试过:

  root.bind('<移-E>',自我pressedCmdE)#工程
root.bind(E,自我。pressedCmdE)#工程
root.bind('<命令E>',自我pressedCmdE)#不起作用
#root.bind('< MOD1-E>',自我pressedCmdE)##做MOD1,M1和
#root.bind('< M1-E>',自我pressedCmdE)##命令意味着同样的事情?


奇怪的是,当我preSS ALT /选项+(E,N,或其他人)它创建了一个错误。难道是与PythonLauncher交互?

  16 2011-06-16:19:22.618的Python [1546:D07]未捕获的异常升高
2011-06-16 16:19:22.621的Python [1546:D07] *** - [NSCFString characterAtIndex:]:范围或索引越界
2011-06-16 16:19:22.622的Python [1546:D07 ***终止应用程序由于未捕获的异常'NSRangeException',原因:'*** - [NSCFString characterAtIndex:]:范围或索引越界
在第一掷***调用堆栈:

    0的CoreFoundation 0x00007fff85b397b4 __exception preprocess + 180
    1 libobjc.A.dylib 0x00007fff848b90f3 objc_exception_throw + 45
    2的CoreFoundation 0x00007fff85b395d7 + NSException提高:格式:参数:] + 103
    3的CoreFoundation 0x00007fff85b39564 + NSException提高:格式:] + 148
    4基金会0x00007fff866eb5e1 - [NSCFString characterAtIndex:] + 97
    5塔卡0x0000000100759bcf Tk_SetCaretPos + 663
    6 TK 0x000000010075fd94 Tk_MacOSXSetupTkNotifier + 699
    7 Tcl的0x000000010061d2ae Tcl_DoOneEvent + 297
    8 _tkinter.so 0x00000001001d9be9 init_tkinter + 1132
    9 Python的0x0000000100089187 PyEval_EvalFrameEx + 15317
    10 Python的0x000000010008acce PyEval_Eval codeEX + 1803
    11 Python的0x000000010008935e PyEval_EvalFrameEx + 15788
    12 Python的0x000000010008acce PyEval_Eval codeEX + 1803
    13 Python的0x000000010008ad61 PyEval_Eval code + 54
    14 Python的0x00000001000a265a Py_CompileString + 78
    15 Python的0x00000001000a2723 PyRun_FileExFlags + 150
    16 Python的0x00000001000a423d PyRun_SimpleFileExFlags + 704
    17 Python的0x00000001000b0286 Py_Main + 2718
    18 Python的0x0000000100000e6c启​​动+ 52

扔NSException的一个实例后终止叫
中止陷阱


解决方案

这似乎是一个塔卡错误。我得到的MAC以及使用Python / Tkinter的的Tcl / Tk同样的错误。您可以绑定<命令E> 来一个小部件(我试着用一个文本组件),但它绑定到根窗口或所有似乎会导致你的错误。

Mac OS X 10.6.6 - Tkinter

I want to bind multiple-key events, and while I have found an effbot article and the Tk man pages, I've been unable to make this work correctly. I'm new here.

I've had mixed success. I've been able to get Shift + letter key, but not Control or Command (Apple key). What I really want to do is Command + letter and Control + letter key so it would theoretically work in Windows and OS X.

I want it to work at window-level, so I'm using root. Perhaps there is a better way. Below is what I've tried:

root.bind('<Shift-E>', self.pressedCmdE)   # Works
root.bind('e', self.pressedCmdE)           # Works
root.bind('<Command-E>', self.pressedCmdE) # Does Not Work
#root.bind('<Mod1-E>', self.pressedCmdE)   #   # Do Mod1, M1, and
#root.bind('<M1-E>', self.pressedCmdE)     #   # Command mean the same thing?


Strangely, when I press alt/option + (E, N, or others) it creates an error. Is it interacting with PythonLauncher?

2011-06-16 16:19:22.618 Python[1546:d07] An uncaught exception was raised
2011-06-16 16:19:22.621 Python[1546:d07] *** -[NSCFString characterAtIndex:]: Range or index out of bounds
2011-06-16 16:19:22.622 Python[1546:d07] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFString characterAtIndex:]: Range or index out of bounds'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x00007fff85b397b4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x00007fff848b90f3 objc_exception_throw + 45
    2   CoreFoundation                      0x00007fff85b395d7 +[NSException raise:format:arguments:] + 103
    3   CoreFoundation                      0x00007fff85b39564 +[NSException raise:format:] + 148
    4   Foundation                          0x00007fff866eb5e1 -[NSCFString characterAtIndex:] + 97
    5   Tk                                  0x0000000100759bcf Tk_SetCaretPos + 663
    6   Tk                                  0x000000010075fd94 Tk_MacOSXSetupTkNotifier + 699
    7   Tcl                                 0x000000010061d2ae Tcl_DoOneEvent + 297
    8   _tkinter.so                         0x00000001001d9be9 init_tkinter + 1132
    9   Python                              0x0000000100089187 PyEval_EvalFrameEx + 15317
    10  Python                              0x000000010008acce PyEval_EvalCodeEx + 1803
    11  Python                              0x000000010008935e PyEval_EvalFrameEx + 15788
    12  Python                              0x000000010008acce PyEval_EvalCodeEx + 1803
    13  Python                              0x000000010008ad61 PyEval_EvalCode + 54
    14  Python                              0x00000001000a265a Py_CompileString + 78
    15  Python                              0x00000001000a2723 PyRun_FileExFlags + 150
    16  Python                              0x00000001000a423d PyRun_SimpleFileExFlags + 704
    17  Python                              0x00000001000b0286 Py_Main + 2718
    18  Python                              0x0000000100000e6c start + 52
)
terminate called after throwing an instance of 'NSException'
Abort trap

解决方案

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.

这篇关于多个关键事件绑定中的Tkinter - &QUOT;控制+ E&QUOT; &QUOT;命令(苹果)+ E&QUOT;等等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆