1from gi.repository import GLib
2from gi.repository import Gtk
3import sys
4
5
6class MyWindow(Gtk.ApplicationWindow):
7    # a window
8
9    def __init__(self, app):
10        Gtk.Window.__init__(self, title="ProgressBar Example", application=app)
11        self.set_default_size(220, 20)
12
13        # a progressbar
14        self.progress_bar = Gtk.ProgressBar()
15        # add the progressbar to the window
16        self.add(self.progress_bar)
17
18        # the method self.pulse is called each 100 milliseconds
19        # and self.source_id is set to be the ID of the event source
20        # (i.e. the bar changes position every 100 milliseconds)
21        self.source_id = GLib.timeout_add(100, self.pulse)
22
23    # event handler
24    # any signal from the keyboard controls if the progressbar stops/starts
25    def do_key_press_event(self, event):
26        # if the progressbar has been stopped (therefore source_id == 0 - see
27        # "else" below), turn it back on
28        if (self.source_id == 0):
29            self.source_id = GLib.timeout_add(100, self.pulse)
30        # if the bar is moving, remove the source with the ID of source_id
31        # from the main context (stop the bar) and set the source_id to 0
32        else:
33            GLib.source_remove(self.source_id)
34            self.source_id = 0
35        # stop the signal emission
36        return True
37
38    # source function
39    # the progressbar is in "activity mode" when this method is called
40    def pulse(self):
41        self.progress_bar.pulse()
42        # call the function again
43        return True
44
45
46class MyApplication(Gtk.Application):
47
48    def __init__(self):
49        Gtk.Application.__init__(self)
50
51    def do_activate(self):
52        win = MyWindow(self)
53        win.show_all()
54
55    def do_startup(self):
56        Gtk.Application.do_startup(self)
57
58app = MyApplication()
59exit_status = app.run(sys.argv)
60sys.exit(exit_status)
61