[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Push to Make (Single Pole) Button running a loop

Jon Todd

12/30/2007 6:05:00 PM

Happy holidays all!

I'm developing an application and, as a relative newbie to Python, have come
across a stumbling block with Tkinter.

I'd like to have a button that when pressed executes a loop (this could be a
thread) and then stops execution when it's released (Push to Make - Single
Pole in electronics terms).

I've tried the regular way of associating the procedure with a callback and
tried using <Button-1> and <ButtonRelease-1> bindings but am getting nowhere
fast (in the latter case the button release event seems to occur anyhows).

So my question is: Is there a neat way of doing this? And would it
necessarily involve creating a custom widget?

....

Also, while I'm here, what's the state of play regarding non-ASCII in
Tkinter widgets. I've been trying to get our currency symbol, £, to display
but didn't have much luck even playing with the coding: settings.

TIA

jon


3 Answers

Dennis Lee Bieber

12/30/2007 9:11:00 PM

0

On Sun, 30 Dec 2007 18:05:26 -0000, "Jon Todd" <jon.todd@zen.co.uk>
declaimed the following in comp.lang.python:

>
> I'd like to have a button that when pressed executes a loop (this could be a
> thread) and then stops execution when it's released (Push to Make - Single
> Pole in electronics terms).
>
We must have separate backgrounds -- that would be a "momentary
contact, normally open" switch to me...

> I've tried the regular way of associating the procedure with a callback and
> tried using <Button-1> and <ButtonRelease-1> bindings but am getting nowhere
> fast (in the latter case the button release event seems to occur anyhows).
>
Minimum runnable sample code would be appreciated...

I suspect your "loop" /will/ need to be a separate thread, which
tests a variable each time through to see if it can continue -- or,
maybe better since you don't want it busy waiting when it is "not
running", use an Event or Condition object. The button callbacks would
set/clear the E/C object, and the loop would block on the object if it
is one of the two states.

If you are trying to work the loop inside the button-down callback,
then the loop is going to block the rest of the GUI from processing
events.
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/

H J van Rooyen

12/31/2007 7:34:00 AM

0

"Jon Todd" <jo...en.co.uk> wrote:

>I'd like to have a button that when pressed executes a loop (this could be a
>thread) and then stops execution when it's released (Push to Make - Single
>Pole in electronics terms).
>
>I've tried the regular way of associating the procedure with a callback and
>tried using <Button-1> and <ButtonRelease-1> bindings but am getting nowhere
>fast (in the latter case the button release event seems to occur anyhows).
>

You are a bit stymied :

Afaik, Button needs the click on the button, leaving it depressed,
while ButtonRelease needs a click on the button, and the release can be anywhere
for drag and drop implementation. So it is possible to call two routines with
one
click and release.

But there is another hassle - you would have to code the loop as a separate
thread,
started by the first, while the second routine sets some variable that is
checked by
the loop to kill itself, as the GUI will not be responsive until the first
command
returns, because the command "captures" the main loop...

HTH - Hendrik


sammy

1/1/2008 6:55:00 PM

0

Hendrik van Rooyen wrote:
> "Jon Todd" <jo...en.co.uk> wrote:
>
>
>>I'd like to have a button that when pressed executes a loop (this could be a
>>thread) and then stops execution when it's released (Push to Make - Single
>>Pole in electronics terms).
>>
>>I've tried the regular way of associating the procedure with a callback and
>>tried using <Button-1> and <ButtonRelease-1> bindings but am getting nowhere
>>fast (in the latter case the button release event seems to occur anyhows).
>>
>
>
> You are a bit stymied :
>
> Afaik, Button needs the click on the button, leaving it depressed,
> while ButtonRelease needs a click on the button, and the release can be anywhere
> for drag and drop implementation. So it is possible to call two routines with
> one
> click and release.
>
> But there is another hassle - you would have to code the loop as a separate
> thread,
> started by the first, while the second routine sets some variable that is
> checked by
> the loop to kill itself, as the GUI will not be responsive until the first
> command
> returns, because the command "captures" the main loop...
>
> HTH - Hendrik
>
>
Perhaps besides all the multithreading issues you should be looking at different kind
of control/button ei. checkbox or radio button (windows terminology)?

My first experience at GUI with Tk/TCL?? was not a good one due to both
lack of docs and fear it wouldn't work on both LUNUX and vindows.

Wish there was a simple library with basic primitives like create_windows resize,
get_client_area and of course some functions to hand of control to the system.
At least for me, that way I could get much further much faster than trying to
find the learn the behavior of all the currently available controls.
Another point I came across recently is that there is no information on proper
cleanup after TCL, don't remember exactly why it was a problem it is after all OOP sy.
So, you may have to read the sourcecode for everything you are using and it ain't easy,

Good Luck.

Cheers.