How can I control keyboard repeat delay in Tkinter root window?

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() 

I drop the up arrow, it prints Hello again and again. However, before this repetition begins, there will be a delay, and the repetition rate will be slower than I want. How to set this repeat delay to zero? How can I control the repeat interval?

I know that other Tkinter widgets have configuration options for 'repeatdelay' and 'repeatinterval', but I cannot find it for the Tkinter root window.

(I look in your direction, Brian Oakley )

+2
source share
2 answers

This is not something configurable in Tk-Tk, it does not control how quickly the keyboard driver sends out repeated key events.

Instead, you can bind to a button by clicking a button and a button to set and then disable the flag. Then you can write a function that does whatever you want, and then check the flag and call yourself again after any delay.

The function will look something like this:

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

To do this, you need a little more logical, but this is a general idea.

+6
source

The following is a complete Brian- based example in this post :

 try: # In order to be able to import tkinter for import tkinter as tk # either in python 2 or in python 3 except ImportError: import Tkinter as tk def step(*event): label['text'] += 1 if label._repeat_on: root.after(label._repeat_freq, step) def stop(*event): if label._repeat_on: label._repeat_on = False root.after(label._repeat_freq + 1, stop) else: label._repeat_on = True if __name__ == '__main__': root = tk.Tk() label = tk.Label(root, text=0) label._repeat_freq = 10 label._repeat_on = True root.bind('<KeyPress-s>', step) root.bind('<KeyRelease-s>', stop) label.pack() root.mainloop() 
+1
source

All Articles