Python Tkinter - 无法绑定箭头键事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19895877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tkinter - Can't bind arrow key events
提问by aftrumpet
I am trying to bind the left and right arrow keys to an event in Tkinter, but when I run the program it appears the events are not triggering. Here is the code:
我试图将左右箭头键绑定到 Tkinter 中的一个事件,但是当我运行该程序时,它似乎没有触发事件。这是代码:
from Tkinter import *
main = Tk()
def leftKey(event):
print "Left key pressed"
def rightKey(event):
print "Right key pressed"
frame = Frame(main, width=100, height=100)
frame.bind('<Left>', leftKey)
frame.bind('<Right>', rightKey)
frame.pack()
frame.mainloop()
Why is this not working?
为什么这不起作用?
采纳答案by Fiver
Try binding to your main variable:
尝试绑定到您的主变量:
from Tkinter import *
main = Tk()
def leftKey(event):
print "Left key pressed"
def rightKey(event):
print "Right key pressed"
frame = Frame(main, width=100, height=100)
main.bind('<Left>', leftKey)
main.bind('<Right>', rightKey)
frame.pack()
main.mainloop()
I should explain that this works because Tk is made aware of the bindings because the main window has keyboard focus. As @BryanOakley's answer explained you could also just set the keyboard focus to the other frame:
我应该解释一下这是有效的,因为 Tk 知道绑定,因为主窗口具有键盘焦点。正如@BryanOakley 的回答所解释的,您也可以将键盘焦点设置为另一帧:
from Tkinter import *
main = Tk()
def leftKey(event):
print "Left key pressed"
def rightKey(event):
print "Right key pressed"
frame = Frame(main, width=100, height=100)
frame.bind('<Left>', leftKey)
frame.bind('<Right>', rightKey)
frame.focus_set()
frame.pack()
main.mainloop()
See more about events and bindings at effbot.
在 effbot 上查看有关事件和绑定的更多信息。
Also, you could also re-write this so your application is a sub-class of Tkinter.Frame
like so:
此外,您还可以重新编写它,使您的应用程序是这样的子类Tkinter.Frame
:
import Tkinter
class Application(Tkinter.Frame):
def __init__(self, master):
Tkinter.Frame.__init__(self, master)
self.master.minsize(width=100, height=100)
self.master.config()
self.master.bind('<Left>', self.left_key)
self.master.bind('<Right>', self.right_key)
self.main_frame = Tkinter.Frame()
self.main_frame.pack(fill='both', expand=True)
self.pack()
@staticmethod
def left_key(event):
print event + " key pressed"
@staticmethod
def right_key(event):
print event + " key pressed"
root = Tkinter.Tk()
app = Application(root)
app.mainloop()
回答by Bryan Oakley
The problem is simply that the frame you are binding to doesn't have the keyboard focus. When you press a key on the keyboard, the event is sent to the widget with the keyboard focus. By default, a frame does not have keyboard focus.
问题很简单,您绑定的框架没有键盘焦点。当您按下键盘上的某个键时,事件将发送到具有键盘焦点的小部件。默认情况下,框架没有键盘焦点。
Add the following line to your code to move the keyboard focus to the frame:
将以下行添加到您的代码以将键盘焦点移动到框架:
frame.focus_set()
回答by mc diamond 144
from tkinter import *
def leftKey(event):
print("Left key pressed")
def rightKey(event):
print("Right key pressed")
main = Tk()
frame = Frame(main, width=100, height=100)
main.bind('<Left>', leftKey)
main.bind('<Right>', rightKey)
frame.pack()
main.mainloop()
回答by Claude
It might be that you don't intercept the right events. The arrows on the numeric keypad and the other ones have different symbolic names.
可能是您没有拦截正确的事件。数字小键盘上的箭头和其他小键盘上的箭头具有不同的符号名称。
See http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/key-names.html
见http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/key-names.html
the ones on the numeric keypad are named with a 'KP_' in front.
数字键盘上的名称前面带有“KP_”。
Hope it helps. Pardon a newbie if not pertinent :-)
希望能帮助到你。如果不相关,请原谅新手:-)