[pygtk] check button state continuously

skip at pobox.com skip at pobox.com
Fri Sep 29 21:25:29 WST 2006


    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


More information about the pygtk mailing list