Adapted from tcaswell's edited answer, but avoiding the extra call to print_hi()
after the button is released:
from gi.repository import Gtk, GObject
class Window(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.button = Gtk.Button("Hi Printer")
self.button.connect("pressed", self.on_button_clicked)
self.button.connect("released", self.on_button_released)
self.add(self.button)
self.connect("delete-event", Gtk.main_quit)
def on_button_clicked(self, widget):
# kick off time out
timeout = 50
self._timeout_id = GObject.timeout_add(timeout, self.print_hi)
def on_button_released(self, widget):
# remove timeout
GObject.source_remove(self._timeout_id)
self._timeout_id = 0 # better safe than sorry
def print_hi(self):
print 'hi'
# repeat until the source is removed
return True
window = Window()
window.show_all()
Gtk.main()