# WildProgressBarGTK.py # 2006-08-21 Copied from the web. # 2006-08-22 This file created. # 2006-08-22 Last modification. import threading import random, time import gtk #Initializing the gtk's thread engine gtk.threads_init() class ProgressBarSetter(threading.Thread): """This class sets the fraction of the progressbar""" def __init__( self, given_progressbar ) : threading.Thread.__init__( self ) self.progressbar_in_window = given_progressbar self.thread_must_be_stopped = threading.Event() print " thread init done " def run( self ) : """Run method, this is the code that runs while thread is alive.""" #While the stopthread event isn't setted, the thread keeps going on while not self.thread_must_be_stopped.isSet() : # Acquiring the gtk global mutex gtk.threads_enter() #Setting a random value for the fraction self.progressbar_in_window.set_fraction(random.random()) # Releasing the gtk global mutex gtk.threads_leave() #Delaying 100ms until the next iteration time.sleep(0.1) print " run method ended" def stop_this_thread( self ) : """Stop method, sets the event to terminate the thread's main loop""" self.thread_must_be_stopped.set() class WildProgressBar : def __init__( self ) : self.application_window = gtk.Window( gtk.WINDOW_TOPLEVEL ) self.application_window.set_title( "PROGRESSBAR IS GIVEN RANDOM VALUES" ) self.application_window.connect( "destroy", self.exit_this_application ) self.application_window.set_size_request( 600, 500 ) progressbar_to_window = gtk.ProgressBar() self.application_window.add( progressbar_to_window ) progressbar_to_window.show() self.application_window.show() self.progressbar_setter = ProgressBarSetter( progressbar_to_window ) self.progressbar_setter.start() print " init done " def run( self ) : gtk.main() def exit_this_application( self, widget, data=None ) : self.progressbar_setter.stop_this_thread() gtk.main_quit() print "exit called" if __name__ == "__main__": this_application = WildProgressBar() this_application.run()