#!/usr/bin/env python # example progressbar.py import gtk # Update the value of the progress bar so that we get # some movement def progress_timeout(pbobj): # Calculate the value of the progress bar using the # value range set in the adjustment object new_val = pbobj.pbar.get_value() + 1 if new_val > pbobj.adj.upper: new_val = pbobj.adj.lower # Set the new value pbobj.pbar.set_value(new_val) # As this is a timeout function, return TRUE so that it # continues to get called return gtk.TRUE class ProgressBar: # Callback that toggles the text display within the progress # bar trough def toggle_show_text(self, widget, data=None): self.pbar.set_show_text(widget.active) # Callback that toggles the activity mode of the progress # bar def toggle_activity_mode(self, widget, data=None): self.pbar.set_activity_mode(widget.active) # Callback that toggles the continuous mode of the progress # bar def set_continuous_mode(self, widget, data=None): self.pbar.set_bar_style(gtk.PROGRESS_CONTINUOUS) # Callback that toggles the discrete mode of the progress # bar def set_discrete_mode(self, widget, data=None): self.pbar.set_bar_style(gtk.PROGRESS_DISCRETE) # Clean up allocated memory and remove the timer def destroy_progress(self, widget, data=None): gtk.timeout_remove(self.timer) self.timer = 0 gtk.mainquit() def __init__(self): self.window = gtk.GtkWindow(gtk.WINDOW_TOPLEVEL) self.window.set_policy(gtk.FALSE, gtk.FALSE, gtk.TRUE) self.window.connect("destroy", self.destroy_progress) self.window.set_title("GtkProgressBar") self.window.set_border_width(0) vbox = gtk.GtkVBox(gtk.FALSE, 5) vbox.set_border_width(10) self.window.add(vbox) vbox.show() # Create a centering alignment object align = gtk.GtkAlignment(0.5, 0.5, 0, 0) vbox.pack_start(align, gtk.FALSE, gtk.FALSE, 5) align.show() # Create a Adjusment object to hold the range of the # progress bar self.adj = gtk.GtkAdjustment(0, 1, 150, 0, 0, 0) # Create the GtkProgressBar using the adjustment self.pbar = gtk.GtkProgressBar(self.adj) # Set the format of the string that can be displayed in the # trough of the progress bar: # %p - percentage # %v - value # %l - lower range value # %u - upper range value self.pbar.set_format_string("%v from [%l-%u] (=%p%%)") align.add(self.pbar) self.pbar.show() # Add a timer callback to update the value of the progress bar self.timer = gtk.timeout_add (100, progress_timeout, self) separator = gtk.GtkHSeparator() vbox.pack_start(separator, gtk.FALSE, gtk.FALSE, 0) separator.show() # rows, columns, homogeneous table = gtk.GtkTable(2, 3, gtk.FALSE) vbox.pack_start(table, gtk.FALSE, gtk.TRUE, 0) table.show() # Add a check button to select displaying of the trough text check = gtk.GtkCheckButton("Show text") table.attach(check, 0, 1, 0, 1, gtk.EXPAND | gtk.FILL, gtk.EXPAND | gtk.FILL, 5, 5) check.connect("clicked", self.toggle_show_text) check.show() # Add a check button to toggle activity mode check = gtk.GtkCheckButton("Activity mode") table.attach(check, 0, 1, 1, 2, gtk.EXPAND | gtk.FILL, gtk.EXPAND | gtk.FILL, 5, 5) check.connect("clicked", self.toggle_activity_mode) check.show() separator = gtk.GtkVSeparator () table.attach(separator, 1, 2, 0, 2, gtk.EXPAND | gtk.FILL, gtk.EXPAND | gtk.FILL, 5, 5) separator.show() # Add a radio button to select continuous display mode button = gtk.GtkRadioButton(None, "Continuous") table.attach(button, 2, 3, 0, 1, gtk.EXPAND | gtk.FILL, gtk.EXPAND | gtk.FILL, 5, 5) button.connect("clicked", self.set_continuous_mode) button.show() # Add a radio button to select discrete display mode button = gtk.GtkRadioButton(button, "Discrete") table.attach(button, 2, 3, 1, 2, gtk.EXPAND | gtk.FILL, gtk.EXPAND | gtk.FILL, 5, 5) button.connect("clicked", self.set_discrete_mode) button.show () separator = gtk.GtkHSeparator() vbox.pack_start(separator, gtk.FALSE, gtk.FALSE, 0) separator.show() # Add a button to exit the program button = gtk.GtkButton("close") button.connect_object("clicked", self.window.destroy, self.window) vbox.pack_start(button, gtk.FALSE, gtk.FALSE, 0) # This makes it so the button is the default. button.set_flags(gtk.CAN_DEFAULT) # This grabs this button to be the default button. Simply hitting # the "Enter" key will cause this button to activate. button.grab_default () button.show() self.window.show() def main(): gtk.mainloop() return 0 if __name__ == "__main__": ProgressBar() main()