Open In App

Python – ProgressBar in GTK+ 3

Improve
Improve
Like Article
Like
Save
Share
Report

ProgressBar is used to display the progress of long-running operations. The Gtk.ProgressBar can be used in two different modes – 

  • Percentage mode
  • Activity mode.

When an application can determine how much work is remaining we can use Gtk.ProgressBar in percentage mode. In this mode, the application is required to call Gtk.ProgressBar.set_fraction() periodically to update the progress bar, passing a float between 0 and 1 to provide the new percentage value. 

When an application cannot determine the amount of work to do, it can use activity mode, which shows activity by a block moving back and forth within the progress area. In this mode, the application is required to call Gtk.ProgressBar.pulse() periodically to update the progress bar. We can choose the step size, with the Gtk.ProgressBar.set_pulse_step() method. By default, Gtk.ProgressBar is horizontal and left-to-right, but you can change it to a vertical progress bar by using the Gtk.ProgressBar.set_orientation() method. Changing the direction the progress bar grows can be done using Gtk.ProgressBar.set_inverted(). Gtk.ProgressBar can also contain text which can be set by calling Gtk.ProgressBar.set_text() and Gtk.ProgressBar.set_show_text().

Example :

Python3




from gi.repository import Gtk, GLib
import gi
  
  
# Since a system can have multiple versions
# of GTK + installed, we want to make
# sure that we are importing GTK + 3.
gi.require_version("Gtk", "3.0")
  
class ProgressBarWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="ProgressBar Demo")
        self.set_border_width(10)
  
        vbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6)
        self.add(vbox)
          
        # Create a ProgressBar
        self.progressbar = Gtk.ProgressBar()
        vbox.pack_start(self.progressbar, True, True, 0)
          
        # Create CheckButton with labels "Show text",
        # "Activity mode", "Right to Left" respectively
        button = Gtk.CheckButton(label="Show text")
        button.connect("toggled", self.on_show_text_toggled)
        vbox.pack_start(button, True, True, 0)
  
        button = Gtk.CheckButton(label="Activity mode")
        button.connect("toggled", self.on_activity_mode_toggled)
        vbox.pack_start(button, True, True, 0)
  
        button = Gtk.CheckButton(label="Right to Left")
        button.connect("toggled", self.on_right_to_left_toggled)
        vbox.pack_start(button, True, True, 0)
  
        self.timeout_id = GLib.timeout_add(50, self.on_timeout, None)
        self.activity_mode = False
  
    def on_show_text_toggled(self, button):
        show_text = button.get_active()
        if show_text:
            text = "Geek For Geeks"
        else:
            text = None
        self.progressbar.set_text(text)
        self.progressbar.set_show_text(show_text)
  
    def on_activity_mode_toggled(self, button):
        self.activity_mode = button.get_active()
        if self.activity_mode:
            self.progressbar.pulse()
        else:
            self.progressbar.set_fraction(0.0)
  
    def on_right_to_left_toggled(self, button):
        value = button.get_active()
        self.progressbar.set_inverted(value)
  
    def on_timeout(self, user_data):
        """
        Update value on the progress bar
        """
        if self.activity_mode:
            self.progressbar.pulse()
        else:
            new_value = self.progressbar.get_fraction() + 0.01
  
            if new_value > 1:
                new_value = 0
  
            self.progressbar.set_fraction(new_value)
        return True
  
  
win = ProgressBarWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()


Output :



Last Updated : 29 Aug, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads