Blame platform-demos/C/samples/progressbar.py

Packit 1470ea
from gi.repository import GLib
Packit 1470ea
from gi.repository import Gtk
Packit 1470ea
import sys
Packit 1470ea
Packit 1470ea
Packit 1470ea
class MyWindow(Gtk.ApplicationWindow):
Packit 1470ea
    # a window
Packit 1470ea
Packit 1470ea
    def __init__(self, app):
Packit 1470ea
        Gtk.Window.__init__(self, title="ProgressBar Example", application=app)
Packit 1470ea
        self.set_default_size(220, 20)
Packit 1470ea
Packit 1470ea
        # a progressbar
Packit 1470ea
        self.progress_bar = Gtk.ProgressBar()
Packit 1470ea
        # add the progressbar to the window
Packit 1470ea
        self.add(self.progress_bar)
Packit 1470ea
Packit 1470ea
        # the method self.pulse is called each 100 milliseconds
Packit 1470ea
        # and self.source_id is set to be the ID of the event source
Packit 1470ea
        # (i.e. the bar changes position every 100 milliseconds)
Packit 1470ea
        self.source_id = GLib.timeout_add(100, self.pulse)
Packit 1470ea
Packit 1470ea
    # event handler
Packit 1470ea
    # any signal from the keyboard controls if the progressbar stops/starts
Packit 1470ea
    def do_key_press_event(self, event):
Packit 1470ea
        # if the progressbar has been stopped (therefore source_id == 0 - see
Packit 1470ea
        # "else" below), turn it back on
Packit 1470ea
        if (self.source_id == 0):
Packit 1470ea
            self.source_id = GLib.timeout_add(100, self.pulse)
Packit 1470ea
        # if the bar is moving, remove the source with the ID of source_id
Packit 1470ea
        # from the main context (stop the bar) and set the source_id to 0
Packit 1470ea
        else:
Packit 1470ea
            GLib.source_remove(self.source_id)
Packit 1470ea
            self.source_id = 0
Packit 1470ea
        # stop the signal emission
Packit 1470ea
        return True
Packit 1470ea
Packit 1470ea
    # source function
Packit 1470ea
    # the progressbar is in "activity mode" when this method is called
Packit 1470ea
    def pulse(self):
Packit 1470ea
        self.progress_bar.pulse()
Packit 1470ea
        # call the function again
Packit 1470ea
        return True
Packit 1470ea
Packit 1470ea
Packit 1470ea
class MyApplication(Gtk.Application):
Packit 1470ea
Packit 1470ea
    def __init__(self):
Packit 1470ea
        Gtk.Application.__init__(self)
Packit 1470ea
Packit 1470ea
    def do_activate(self):
Packit 1470ea
        win = MyWindow(self)
Packit 1470ea
        win.show_all()
Packit 1470ea
Packit 1470ea
    def do_startup(self):
Packit 1470ea
        Gtk.Application.do_startup(self)
Packit 1470ea
Packit 1470ea
app = MyApplication()
Packit 1470ea
exit_status = app.run(sys.argv)
Packit 1470ea
sys.exit(exit_status)