gpt4 book ai didi

python - Tkinter KeyPress 和 KeyRelease 事件

转载 作者:IT老高 更新时间:2023-10-28 22:22:38 26 4
gpt4 key购买 nike

我知道 Tk keypress 和 keyrelease 事件应该只在实际按下或释放键时触发?

但是,使用以下简单代码,如果我按住“a”键,我会得到一个连续的交替按键/按键释放事件序列。

是我做错了什么还是 TkInter 有问题?这是 Linux mint 上的 Python2.7。

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...

最佳答案

好吧,发现更多研究 this helpful post这表明这是由于 X 的自动重复行为而发生的。您可以使用禁用此功能

os.system('xset r off')

然后在脚本末尾使用“on”重置它。问题是这是全局行为——不仅仅是我的脚本——这不是很好,所以我希望有人能想出更好的方法。

关于python - Tkinter KeyPress 和 KeyRelease 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27215326/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com