# MovingBallWithMouseQt.py Copyright (c) Kari Laitinen # http://www.naturalprogramming.com # 2010-09-28 File created. # 2010-09-28 Last modification. # This program draws three Ball objects onto the screen. # It is possible to move these balls by dragging them with # the mouse. The Ball class provides a method with which it is # possible to find out whether a clicked point is inside the # ball. In addition the Ball class provides methods for moving # the ball. # During compilation/execution the file BallQt.py must be in the same # folder (directory) with this file. import sys from PyQt4.QtCore import * from PyQt4.QtGui import * from BallQt import Ball class MovingBallWithMouseWindow( QWidget ) : def __init__( self, parent = None ) : QWidget.__init__( self, parent ) self.setGeometry( 200, 200, 680, 480 ) self.setWindowTitle( "MOVE THE BALLS WITH THE MOUSE" ) window_width = self.width() window_height = self.height() # The coordinates that are given to Ball constructor refer to # the center point of a ball. self.first_ball = Ball( window_width / 2 - 120, window_height / 2, Qt.red ) self.second_ball = Ball( window_width / 2, window_height / 2, Qt.green ) self.third_ball = Ball( window_width / 2 + 120, window_height / 2, Qt.blue ) self.ball_being_moved = None self.previous_cursor_position_x = 0 self.previous_cursor_position_y = 0 def mousePressEvent( self, event ) : cursor_position_x = event.x() cursor_position_y = event.y() if self.first_ball.contains_point( cursor_position_x, cursor_position_y ) : self.ball_being_moved = self.first_ball self.ball_being_moved.activate_ball() elif self.second_ball.contains_point( cursor_position_x, cursor_position_y ) : self.ball_being_moved = self.second_ball self.ball_being_moved.activate_ball() elif self.third_ball.contains_point( cursor_position_x, cursor_position_y ) : self.ball_being_moved = self.third_ball self.ball_being_moved.activate_ball() self.previous_cursor_position_x = cursor_position_x self.previous_cursor_position_y = cursor_position_y self.update() def mouseMoveEvent( self, event ) : if self.ball_being_moved != None : new_cursor_position_x = event.x() new_cursor_position_y = event.y() mouse_movement_x = int( new_cursor_position_x ) \ - self.previous_cursor_position_x mouse_movement_y = int( new_cursor_position_y ) \ - self.previous_cursor_position_y self.previous_cursor_position_x = int( new_cursor_position_x ) self.previous_cursor_position_y = int( new_cursor_position_y ) self.ball_being_moved.move_this_ball( mouse_movement_x, mouse_movement_y ) self.update() def mouseReleaseEvent( self, event ) : if self.ball_being_moved != None : self.ball_being_moved.deactivate_ball() self.ball_being_moved = None self.update() def paintEvent( self, event ) : painter = QPainter() painter.begin( self ) self.first_ball.draw( painter ) self.second_ball.draw( painter ) self.third_ball.draw( painter ) painter.end() # The main program begins. this_application = QApplication( sys.argv ) application_window = MovingBallWithMouseWindow() application_window.show() this_application.exec_()