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

2017-07-10 Thread Alan Gauld via Tutor
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("",do_down)
l.bind("",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


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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

2017-07-04 Thread Carlton Banks
> 
>> Interesting solution, but still find a bit "dirty hackish”
>> to involve a timer in this..  I guess it just would be neat if 
>> it just worked as i thought it to be.  But i guess i have to look into 
>> curses.
> 
> A timer based loop is actually the standard pattern for
> implementing a loop in an event driven environment (the
> main alternative is to launch a background thread).
> It's how most GUIs handle such scenarios.
> 
> The problem in a CLI solution is that you have to build your
> own timer event system (usually based on time.sleep() or
> similar). Hence the suggestion to use a GUI.
> 

Any suggestion on any GUI solutions?


Regards 
Carl

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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

2017-07-04 Thread Alan Gauld via Tutor
On 04/07/17 12:00, Alan Gauld via Tutor wrote:

>> the library you are using, I normally just use the standard
>> library for such things, or build a simple GUI…
> 
> Standard library being?.. 

The Python standard library that ships with Python and
is documented on python.org. As in:

>> There are several but it depends on your OS.
>> In the standard library
>> Unix variants can use curses.
>> Windows users have msvcrt.


> Interesting solution, but still find a bit "dirty hackish”
> to involve a timer in this..  I guess it just would be neat if 
> it just worked as i thought it to be.  But i guess i have to look into curses.

A timer based loop is actually the standard pattern for
implementing a loop in an event driven environment (the
main alternative is to launch a background thread).
It's how most GUIs handle such scenarios.

The problem in a CLI solution is that you have to build your
own timer event system (usually based on time.sleep() or
similar). Hence the suggestion to use a GUI.

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


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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

2017-07-04 Thread Alan Gauld via Tutor
Forwarding to Tutor list


Please always use reply-All or reply-List to respond to tutor posts.



 Forwarded Message 

> Den 4. jul. 2017 kl. 11.35 skrev Alan Gauld via Tutor :
> 
> On 04/07/17 09:50, Carlton Banks wrote:
>> I am trying to record from my microphone while i press down a button,
> 
> First question is which OS?
> That makes a big difference in this kind of scenario.

I am currently testing it on OS X, but should also be working on a ubuntu 
machine.  

>> the library I am using is not able to detect on hold event. 
> 
> There isn't really any concept of a hold event, you usually
> have to detect repeated press events. However I don;t know
> the library you are using, I normally just use the standard
> library for such things, or build a simple GUI…

Standard library being?.. 

> 
>> It only detect on press, which happens once, 
> 
> That's unusual, usually you get a repeated stream of
> press events when you hold a key down. But it does
> depend on the OS and environment. Your library may
> indeed just be detecting the initial key down and
> be waiting for the key up event.

Hmm.. interesting but again it is currently being tested on OSX,
as it was the only machine available atm. 
> 
>> So does any of you know any libraries that monitor> state of the keyboard,
> 
> There are several but it depends on your OS.
> In the standard library
> Unix variants can use curses.
> Windows users have msvcrt.
> 
> And there are several 3rd party libraries that try
> to do in a platform independent way - I'm guessing
> the one you are using is one such.
> 
> Another way to tackle it is to switch your code so
> it works with a toggle. Set the toggle to on in
> the on_press handler. Set it to off in the
> on_release handler
> 
> Then trigger a timer loop that runs for as long
> as the toggle is set. This is probably easiest
> done within a GUI.
> 

Interesting solution, but still find a bit "dirty hackish”
to involve a timer in this..  I guess it just would be neat if 
it just worked as i thought it to be.  But i guess i have to look into curses.



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor