Gtk3 ProgressBar (): unable to receive events in Python

I am trying to port an audio player written in python to GTK3 +. In GTK2, I used progress_bar.add_event (... pointer_motion_notify | button_press) (full code below) and installed a signal handler for button_press and pointer_motion_notify. In GTK3, it seems that the ProgressBar () does not emit these signals.

I applied a workaround using Overlay () and DrawingArea (), which allows DrawingArea to emit signals, but shouldn't ... Is this a bug? or am i doing it wrong?

code:

import gi gi.require_version("Gtk","3.0") from gi.repository import Gtk, Gdk, GObject class MainWindow(Gtk.Window): def __init__(self): super(MainWindow, self).__init__(title='ProgressBar Event Test') self.progressbar = Gtk.ProgressBar() self.add(self.progressbar) self.progressbar.set_events(Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.POINTER_MOTION_MASK) self.progressbar.connect("button-press-event", self.on_event, 'button-press') self.progressbar.connect("motion-notify-event", self.on_event, 'motion-notify') self.connect("delete-event", Gtk.main_quit) self.current_progress = 0.0 GObject.timeout_add(200,self.update_progress) self.show_all() def on_event(self, widget, event, data=None): print "on_event called for %s signal"%data return False def update_progress(self): self.progressbar.set_fraction(self.current_progress) self.current_progress += 0.01 return self.current_progress <= 1 # False cancels timeout def main(): w = MainWindow() Gtk.main() if __name__ == '__main__': main() 
+5
source share
1 answer

Most likely, it is better to use eventbox - you add a widget to the event field and connect to the events themselves.

In this way:

 import gi gi.require_version("Gtk","3.0") from gi.repository import Gtk, Gdk, GObject class MainWindow(Gtk.Window): def __init__(self): super(MainWindow, self).__init__(title='ProgressBar Event Test') eventbox = Gtk.EventBox() self.progressbar = Gtk.ProgressBar() eventbox.add(self.progressbar) self.add(eventbox) eventbox.set_events(Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.POINTER_MOTION_MASK) eventbox.connect("button-press-event", self.on_event, 'button-press') eventbox.connect("motion-notify-event", self.on_event, 'motion-notify') self.connect("delete-event", Gtk.main_quit) self.current_progress = 0.0 GObject.timeout_add(200,self.update_progress) self.show_all() def on_event(self, widget, event, data=None): print "on_event called for %s signal"%data return False def update_progress(self): self.progressbar.set_fraction(self.current_progress) self.current_progress += 0.01 return self.current_progress <= 1 # False cancels timeout def main(): w = MainWindow() Gtk.main() if __name__ == '__main__': main() 

More information about Gtk.Eventbox can be found here .

+4
source

Source: https://habr.com/ru/post/1236964/


All Articles