# AnimationDemoQt.py (c) Kari Laitinen # http://www.naturalprogramming.com/ # 2009-11-30 File created. # 2009-11-30 Last modification. # This program shows how simple animation can be produced with # a thread. The 'animation' here is a blinking ball on the screen. import sys from PyQt4.QtCore import * from PyQt4.QtGui import * from time import sleep class AnimationThread ( QThread ) : def __init__( self ) : QThread.__init__( self ) self.thread_must_be_run = True self.mutex = QMutex() # print " thread init done " def stop_this_thread( self ) : self.thread_must_be_run = False def run( self ) : while self.thread_must_be_run == True : self.mutex.lock() # locking may not be necessary # Here we emit a signal whenever we want to update window # contents. The other thread will handle this signal # so that the method process_window_update_request() will # be called. self.emit( SIGNAL( "window_update_request" ) ) self.mutex.unlock() sleep( 1.000 ) # Delay of one second. #QThread.sleep( 1 ) # An alternative way to sleep. # print " run method ends " class AnimationDemoWindow( QWidget ) : def __init__( self, parent=None ) : QWidget.__init__( self, parent ) self.setGeometry( 100, 100, 600, 500 ) self.setWindowTitle( "ANIMATION BY USING A THREAD" ) window_width = self.width() window_height = self.height() self.ball_center_point_x = window_width / 2 ; self.ball_center_point_y = window_height / 2 ; self.ball_must_be_shown = False self.animation_thread = AnimationThread() # The following statement specifies that when the # animation thread emits the window_update_request signal, # the method self.process_window_update_request will be # called automatically. self.connect( self.animation_thread, SIGNAL( "window_update_request" ), self.process_window_update_request ) # After the following method call, the program execution # system will automatically call the run() method of the # AnimationThread class. The run() method is executed # in parallel with "this" thread. self.animation_thread.start() def process_window_update_request( self ) : self.update() def paintEvent( self, event ) : if self.ball_must_be_shown == True : painter = QPainter() painter.begin( self ) painter.setBrush( Qt.cyan ) painter.drawEllipse( self.ball_center_point_x - 50, self.ball_center_point_y - 50, 100, 100 ) painter.end() self.ball_must_be_shown = False else : self.ball_must_be_shown = True def exit_this_window( self ) : self.animation_thread.stop_this_thread() self.close() #is this necessary??? # Here is the main program: def main( args ) : this_application = QApplication( args ) application_window = AnimationDemoWindow() application_window.show() # The following statement specifies that when the # "lastWindowClosed()" signal is emitted by QApplication, # the Python method exit_this_window is called for the # PictureShowWindow object. this_application.connect( this_application, SIGNAL( "lastWindowClosed()" ), application_window.exit_this_window ) this_application.exec_() if __name__== "__main__" : main( sys.argv )