# PictureShowQt.py (c) Kari Laitinen # http://www.naturalprogramming.com/ # 2009-11-16 File created. # 2009-11-30 Last modification. # This program shows a sequence of pictures, changing the # picture after each 3 seconds. # A thread is used to time the picture change. # The picture files must be in the same folder where this # file is located. 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() # I do not know if this really is useful. # 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 a new picture # to be drawn. The other thread will handle this signal # so that the method change_picture_and_redraw() will # be called. self.emit( SIGNAL( "picture_change_request" ) ) self.mutex.unlock() sleep( 3.000 ) # Delay of three seconds. #QThread.sleep( 3 ) # An alternative way to sleep. # print " run method ends " class PictureShowWindow( QWidget ) : def __init__( self, parent=None ) : QWidget.__init__( self, parent ) self.setGeometry( 100, 100, 800, 600 ) self.setWindowTitle( "SHOWING A SEQUENCE OF PICTURES" ) picture_file_names = ( "yellow_field_by_vincent_van_gogh.jpg", "night_watch_by_rembrandt.jpg", "persistence_of_memory_by_dali.jpg", "demoiselles_de_avignon_by_picasso.jpg", "mona_lisa_by_leonardo.jpg" ) self.pictures_to_be_shown = [] for file_name in picture_file_names : picture_to_be_shown = QImage( file_name ) self.pictures_to_be_shown.append( picture_to_be_shown ) self.index_of_current_picture = 0 self.animation_thread = AnimationThread() self.connect( self.animation_thread, SIGNAL( "picture_change_request" ), self.change_picture_and_redraw ) self.animation_thread.start() def change_picture_and_redraw( self ) : self.index_of_current_picture += 1 if self.index_of_current_picture >= len( self.pictures_to_be_shown ) : self.index_of_current_picture = 0 self.update() def paintEvent( self, event ) : painter = QPainter() painter.begin( self ) painter.drawImage( 30, 20, self.pictures_to_be_shown[ self.index_of_current_picture ] ) painter.end() 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 = PictureShowWindow() 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 ) # Notes: # There are two methods for redrawing the screen area. # These are repaint() and update(). Qt Documentation says the # following about the use of these methods. # We suggest only using repaint() if you need an immediate repaint, # for example during animation. In almost all circumstances update() # is better, as it permits Qt to optimize for speed and minimize flicker. #http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qwidget.html#repaint