There is any event for a Gtk.Button that would execute the code repeatedly while the button is being pressed?
Let's supose I have the following code written in Python 3 and using PyGObject. I'd like to have the message "Hi" been repeadly printed on the screen while the user is holding down the mouse left button over the button (clicking and holding).
There is any other event I can use instead of clicked or any other solution? Thanks.
from gi.repository import Gtk
class Window(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.button = Gtk.Button("Hi Printer")
self.button.connect("clicked", self.on_button_clicked)
self.add(self.button)
self.connect("delete-event", Gtk.main_quit)
def on_button_clicked(self, widget):
print("Hi")
window = Window()
window.show_all()
Gtk.main()
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()