# ClickingsQt.java # http://www.naturalprogramming.com/ # 2009-10-30 File received from Esa Pesonen. # 2009-10-30 Last modification by Kari Laitinen # This program shows a window which you can click with the mouse. # The program 'draws' the clicked points to the screen. # This program was originally transformed from a PyGTK application # to a PyQt application by Esa Pesonen. # http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qwidget.html # http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qmouseevent.html import sys from PyQt4.QtCore import * from PyQt4.QtGui import * class ClickingsWindow( QWidget ) : def __init__( self, parent=None ) : QWidget.__init__( self, parent ) self.setGeometry( 300, 300, 600, 400 ) self.setWindowTitle( "CLICK THIS WINDOW WITH THE MOUSE" ) self.list_of_clicking_coordinates = [] # We'll override the mousePressEvent() method (inherited from QWidget) def mousePressEvent( self, event ) : self.list_of_clicking_coordinates.append( ( event.x() , event.y() ) ) self.update() def paintEvent( self, event ) : painter = QPainter() painter.begin( self ) painter.setBrush( Qt.green ) for clicking_coordinates in self.list_of_clicking_coordinates : # Below 0 refers to the x coordinate and 1 refers to # the y coordinate. These are stored in a tuple whose length is 2. coordinates_as_text = ( "(%d, %d)" % clicking_coordinates ) painter.drawEllipse( clicking_coordinates[ 0 ] - 3, clicking_coordinates[ 1 ] - 3, 6 , 6 ) painter.drawText( clicking_coordinates[ 0 ] + 7, clicking_coordinates[ 1 ] + 5, coordinates_as_text) painter.end() # The main program begins. this_application = QApplication( sys.argv ) application_window = ClickingsWindow() application_window.show() this_application.exec_()