# AnimationDemoGTKnoclass.py (c) Kari Laitinen # 2006-08-21 File created. # 2009-02-25 Last modification. # A no-class version of the program. # Keyword 'global' demonstrated. import pygtk pygtk.require('2.0') import gtk from threading import Thread from threading import Event from time import sleep #Initializing the gtk's thread engine gtk.gdk.threads_init() class AnimationThread ( Thread ) : def __init__( self, given_drawing_area ) : Thread.__init__( self ) self.external_drawing_area = given_drawing_area self.thread_must_be_stopped = Event() print " thread init done " def stop_this_thread( self ) : self.thread_must_be_stopped.set() def run( self ) : while not self.thread_must_be_stopped.isSet() : # Acquiring the gtk global mutex gtk.gdk.threads_enter() self.external_drawing_area.queue_draw() # Releasing the gtk global mutex gtk.gdk.threads_leave() sleep( 1.000 ) # Delay of one second. print " xxx ", print " run method ends " def drawing_area_exposed( area, event ) : print "drawing_area_exposed called" global ball_must_be_shown global drawing_area if ball_must_be_shown == True : graphics_context = \ drawing_area.get_style().fg_gc[gtk.STATE_NORMAL] this_drawable = drawing_area.window # Note that angles are expressed in 1/64ths of a degree. # 360 degrees is thus represented by value 360*64 this_drawable.draw_arc( graphics_context, True, 200, 200, 100, 100, 0, 360*64 ) ball_must_be_shown = False else : ball_must_be_shown = True return True def exit_this_application( widget, data=None ) : global animation_thread ; animation_thread.stop_this_thread() gtk.main_quit() print "exit called" application_window = gtk.Window( gtk.WINDOW_TOPLEVEL ) application_window.set_title( "ANIMATION BY USING A THREAD" ) application_window.connect( "destroy", exit_this_application ) application_window.set_size_request( 600, 500 ) drawing_area = gtk.DrawingArea() application_window.add( drawing_area ) drawing_area.connect( "expose-event", drawing_area_exposed ) drawing_area.show() application_window.show() ball_must_be_shown = True animation_thread = AnimationThread( drawing_area ) animation_thread.start() print " init done " gtk.main()