Adding progress bars & spinners



tut05-progress-spinners.png

Figure 1: 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.

#!/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()

Comments