Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gtk3 ProgressBar(): unable to receive events in Python

Tags:

python

gtk3

I'm trying to migrate 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 set a signal handler for button_press and pointer_motion_notify. In GTK3, it appears that ProgressBar() does not emit these signals.

I have implemented a workaround using Overlay() and DrawingArea() which allows the DrawingArea to emit the signals, but should'nt need to... 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()
like image 683
simonltwick Avatar asked Sep 26 '22 02:09

simonltwick


1 Answers

Its probably better to use an eventbox - you add the widget to the eventbox and connect to the eventbox signals themselves.

Thus:

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 info about the Gtk.Eventbox can be found here.

like image 140
fossfreedom Avatar answered Oct 11 '22 17:10

fossfreedom