Tkinter 中的多个按键事件绑定 - “控制” E” “命令(苹果)” E” ETC
Mac OS X 10.6.6 - Tkinter
我想绑定多键事件,虽然我找到了 effbot 文章和 Tk 手册页,但我一直无法使其正常工作。我是新来的。
我的成功有好有坏。我可以使用 Shift + 字母键,但不能使用 Control 或 Command(Apple 键)。我真正想做的是 Command + 字母和 Control + 字母键,这样理论上它可以在 Windows 和 OS X 中工作。
我希望它在窗口级别工作,所以我使用 root。也许有更好的方法。以下是我尝试过的:
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?
奇怪的是,当我按 alt/option +(E、N 或其他)时,它会产生错误。它与 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于 Tkinter,“Control-R”表示 Ctrl-Shift-R 而“Control-r”表示 Ctrl -R。因此,请确保您没有混淆大写和小写。
With Tkinter, "Control-R" means Ctrl-Shift-R whereas "Control-r" means Ctrl-R. So make sure you're not mixing up uppercase and lowercase.
这似乎是 Tk 中的一个错误。我在 mac 上使用 tcl/tk 以及 python/tkinter 时都会遇到同样的错误。您可以将
绑定到小部件(我尝试使用文本小部件),但将其绑定到根窗口或"all"
似乎会导致你得到的错误。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.增强功能涵盖了
Alt
和Meta
键,即 macOS 上的Option
和Command
。Enhanced to cover the
Alt
andMeta
keys, akaOption
andCommand
on macOS.选项 1
像这样:
选项 2
但是,最终,我决定手动处理击键。您可以在此文件中查看示例。首先,我在两个字典
self.keycode
和self.__shortcuts
中设置键码和快捷键:然后添加
self.__keytrip
函数来监控
击键事件。该函数检查
键是否被按下:最后,将
self.__keyrinkle
函数绑定到主 GUI 窗口。请注意,此函数在空闲模式下绑定,因为多次击键会减慢弱计算机上的程序速度:Option 1
Something like this:
Option 2
However, in the end, I decided to process keystrokes manually. You can se the example in this file. First, I set keycodes and shortcuts in two dictionaries
self.keycode
andself.__shortcuts
:Then added
self.__keystroke
function to monitor<Ctrl>
keystroke events. This function checks if<Ctrl>
key is pressed or not:Finally, bind the
self.__keystroke
function to the master GUI window. Note that this function is bonded in the idle mode, because multiple keystrokes slow down the program on weak computers: