[Tutor] Fwd: Re: call key on_press event multiple times when key is held down

Alan Gauld alan.gauld at yahoo.co.uk
Mon Jul 10 05:10:15 EDT 2017


On 04/07/17 13:45, Carlton Banks wrote:

> Any suggestion on any GUI solutions?

Here is a Tkinter solution that increments a counter
while the mouse button is pressed. It should give you the idea...
Obviously you need to replace the counter increment
with your desired processing. And the print statements
are just to show the events being processed.

##################################
import tkinter as tk

display = "count: "
flag = False
counter = 0

def do_down(e):
    global flag
    print('Key down')
    flag = True
    after_loop()

def do_up(e):
    global flag
    print('key up')
    flag = False

def after_loop():
    print('looping')
    global counter
    counter += 1
    l['text'] = display +str(counter)
    if flag:
       top.after(10,after_loop)


top = tk.Tk()
l = tk.Label(top,text=display+'0')
l.pack()
l.bind("<Button-1>",do_down)
l.bind("<ButtonRelease-1>",do_up)
top.mainloop()

###################################

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list