[pygtk] check button state continuously

Karsten Henke henke at robolab.de
Fri Sep 29 21:40:34 WST 2006


skip at pobox.com schrieb:
>     Karsten> i try to write a button callback, that repeatedly calls a
>     Karsten> function, as long as the button is pressed. For security
>     Karsten> reasons i don't want to wait for the released signal, but check
>     Karsten> the button state continuously. My code would look like this:
>
>     Karsten> while (button_is_pressed):
>     Karsten>     move_robot()
>
> How continuous is "continuous"?  You could use gobject.timeout_add with a
> short timeout to check the button state and execute the move_robot()
> method.  Something like this:
>
>     class RobotThing:
>         def __init__(self):
>             self.move_id = 0
>             self.move_button = gtk.Button(label="Move Robot")
>             self.move_button.connect("pressed", self.on_button_pressed)
>             self.move_button.connect("released", self.on_button_released)
>
>         def on_button_pressed(self, b):
>             self.move_id = gobject.timeout_add(50, self.move_robot)
>
>         def on_button_released(self, b):
>             gobject.source_remove(self.move_id)
>             self.move_id = 0
>
>         def move_robot(self):
>             # do a little dance ...
>             # make a little love ...
>             # get down tonight!
>             # get down tonight!
>             pass # out
>
> Skip
>   
Thx for your answer. Basically i was thinking of something similar. 
Creating a thread and waiting for the release signal. But because the 
move_robot function is critical, i wanted to have some security if the 
release event got lost. So i would prefer a solution, where i can 
_actively_ check the button state - in oposite to the passive solution, 
that waits for a signal. Is something like that possible?


More information about the pygtk mailing list