# PictureShowQt.py (c) Kari Laitinen # http://www.naturalprogramming.com/ # 2009-11-16 File created. # 2009-11-23 Last modification. import sys from PyQt4 import QtGui, QtCore from threading import Thread from threading import Event from time import sleep ##gtk.gdk.threads_init() # Initializing the gtk's thread engine class AnimationThread ( Thread ) : def __init__( self, given_main_thread_object ) : Thread.__init__( self ) self.main_thread_object = given_main_thread_object self.thread_must_be_stopped = Event() self.mutex = QtCore.QMutex() # Lisäsin tämän mutta ei auttanut. # 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() : #gtk.gdk.threads_enter() # Acquiring the gtk global mutex self.mutex.lock() self.main_thread_object.change_picture_and_redraw() self.mutex.unlock() #gtk.gdk.threads_leave() # Releasing the gtk global mutex sleep( 3.000 ) # Delay of three seconds. # print " run method ends " class PictureShowWindow( QtGui.QWidget ) : def __init__( self, parent=None ) : QtGui.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 = QtGui.QImage( file_name ) self.pictures_to_be_shown.append( picture_to_be_shown ) self.index_of_current_picture = 0 self.animation_thread = AnimationThread( self ) 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.repaint() #QtGui.QWidget.repaint( self ) # an alternative method call # Should I use update() in place of repaint() ??? #http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qwidget.html#repaint def paintEvent( self, event ) : painter = QtGui.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 = QtGui.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, QtCore.SIGNAL( "lastWindowClosed()" ), application_window.exit_this_window ) this_application.exec_() if __name__== "__main__" : main( sys.argv ) # Notes: # 2009-11-23: This program worked in Windows but not in Ubuntu Linux. # The Linux error message said that a signal cannot be sent # from one thread to another. # Must try Qt threads. See ThreadTestQt.py.