TkInter 按键、按键释放事件 [英] TkInter keypress, keyrelease events
问题描述
我知道 Tk keypress 和 keyrelease 事件应该只在实际按下或释放键时触发?
I understood that the Tk keypress and keyrelease events were supposed only to fire when the key was actually pressed or released?
但是,通过下面的简单代码,如果我按住a"键,我会得到一个连续的按键/按键释放事件交替序列.
However with the following simple code, if I hold down the "a" key I get a continual sequence of alternating keypress/keyrelease events.
我做错了什么还是 TkInter 有问题?这是 Linux mint 上的 Python2.7.
Am I doing something wrong or is TkInter buggy? This is Python2.7 on Linux mint.
from Tkinter import *
def keyup(e):
print 'up', e.char
def keydown(e):
print 'down', e.char
root = Tk()
frame = Frame(root, width=100, height=100)
frame.bind("<KeyPress>", keydown)
frame.bind("<KeyRelease>", keyup)
frame.pack()
frame.focus_set()
root.mainloop()
按住a"时输出:
down a
up a
down a
up a
down a
up a
down a
up a
etc...
推荐答案
好的,还有一些研究发现 这篇有用的帖子 表明这是由于 X 的自动重复行为而发生的.您可以使用
Ok some more research found this helpful post which shows this is occuring because of X's autorepeat behaviour. You can disable this by using
os.system('xset r off')
然后在脚本末尾使用on"重置它.问题是这是全局行为 - 不仅仅是我的脚本 - 这不是很好,所以我希望有人能想出更好的方法.
and then reset it using "on" at the end of your script. The problem is this is global behaviour - not just my script - which isn't great so I'm hoping someone can come up with a better way.
这篇关于TkInter 按键、按键释放事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!