如何在Tkinter根窗口中控制键盘重复延迟? [英] How can I control keyboard repeat delay in a Tkinter root window?

查看:119
本文介绍了如何在Tkinter根窗口中控制键盘重复延迟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个简单的应用程序几乎可以满足我的要求:

This simple application almost does what I want:

import Tkinter as Tk

def hello(x):
    print "Hello"

root = Tk.Tk()
root.bind("<Up>", hello)
root.mainloop()

我向下按向上箭头,一遍又一遍地打印"Hello".但是,此重复开始之前有一个延迟,并且重复率比我想要的要慢. 如何将此重复延迟设置为零?如何控制重复间隔?

I mash down the up arrow, it prints "Hello" over and over. However, there is a delay before this repetition begins, and the repetition rate is slower than I want. How can I set this repeat delay to zero? How can I control the repeat interval?

我知道其他Tkinter小部件具有"repeatdelay"和"repeatinterval"的配置选项,但是我似乎找不到Tkinter根窗口的一个.

I know that other Tkinter widgets have configuration options for 'repeatdelay' and 'repeatinterval', but I can't seem to find one for a Tkinter root window.

(我正在寻找您的方向, Bryan Oakley )

(I'm looking in your direction, Bryan Oakley)

推荐答案

在Tk中这不是可配置的-Tk无法控制键盘驱动程序发送重复按键事件的速度.

This is not something configurable in Tk -- Tk has no control over how fast the keyboard driver sends repeated key events.

您可以做的是在按钮按下和按钮释放之间进行绑定,以设置然后取消设置标志.然后,您可以编写一个函数,执行所需的操作,然后检查该标志并在所需的延迟后再次调用自身.

What you can do instead is have a binding on the button press and button release to set and then unset a flag. Then, you can write a function that does whatever you want it to do, then check the flag and call itself again after whatever delay you want.

函数看起来像这样:

def hello(x):
    global SHOULD_REPEAT
    print "hello"
    if SHOULD_REPEAT:
        root.after(10, hello) # wait 10ms then repeat

要做正确的事需要更多的逻辑,但这是一般的想法.

To do it right requires a little bit more logic, but that's the general idea.

这篇关于如何在Tkinter根窗口中控制键盘重复延迟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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