Adding progress bars & spinners
rss_feed

Adding progress bars & spinners

homeHome
pagesGTK-3
pagespython
pageswidgets

Progress bars and spinners

This sample demonstrates two progress bar styles and how to update them too show progress, it also demonstrates starting and stopping a spinning graphic.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
from gi.repository import Gtk, GLib


class application_gui:
    """Tutorial 05 demo progress bars and the spinner graphic."""
    count = 0

    def __init__(self):
        #load in our glade interface
        xml = Gtk.Builder()
        xml.add_from_file('tut05.glade')

        #grab our widget using get_object this is the name of the widget from glade, window1 is the default name
        self.window = xml.get_object('window1')
        self.text = xml.get_object('entry1')

        #load our widgets from the glade file
        self.widgets = {}
        self.widgets['entry'] = xml.get_object('entry2')
        
        self.widgets['spinner1'] = xml.get_object('spinner1')
        self.widgets['spinnerbutton'] = xml.get_object('togglebutton1')
        self.widgets['progress1'] = xml.get_object('progressbar1')
        self.widgets['progress2'] = xml.get_object('progressbar2')

        self.widgets['spinnerbutton'].connect('clicked', self.toggle_spinner)

        self.widgets['progress1'].set_text('active mode')
        self.widgets['progress2'].set_text('percentage mode')

        #connect to events, in this instance just quit our application
        self.window.connect('delete_event', Gtk.main_quit)
        self.window.connect('destroy', lambda quit: Gtk.main_quit())

        #show the window else there is nothing to see :)
        self.window.show()
        self.widgets['spinner1'].start()
        
        GLib.timeout_add_seconds(1, self.update_active_progress_bar)
        GLib.timeout_add_seconds(1, self.update_percentage_progress_bar)

    def update_active_progress_bar(self):
        # nudge the progress bar to show something is still happening, just updating every second in the example
        # long running processes which have an unknow finish time would use this version
        self.widgets['progress1'].pulse()
        return True

    def update_percentage_progress_bar(self):
        self.count +=0.1
        self.widgets['progress2'].set_fraction(self.count)
        if self.count<1:
            return True
        else:
            return False

    def toggle_spinner(self, widget):
        if widget.get_active():
            self.widgets['spinner1'].start()
        else:
            self.widgets['spinner1'].stop()


application = application_gui()
Gtk.main()