# MovingBallQt.py # http://www.naturalprogramming.com # 2009-11-18 First program version created by Jaakko Partanen # 2010-09-27 Last modification by Kari Laitinen # This program was transformed to a PyQt application by Jaakko # Partanen. # Color names used here are described at # http://www.w3.org/TR/SVG/types.html#ColorKeywords # It should be possible to create QColor objects with these names # on all Qt platforms. import sys from PyQt4.QtCore import * from PyQt4.QtGui import * class MovingBallWindow( QWidget ) : def __init__( self, parent = None ) : QWidget.__init__( self, parent ) self.setWindowTitle( "Move the Ball with Buttons" ) self.setGeometry( 100, 100, 600, 500 ) left_button = QPushButton( "Left", self ) up_button = QPushButton( "Up", self ) down_button = QPushButton( "Down", self ) right_button = QPushButton( "Right" , self ) self.connect( left_button, SIGNAL( "clicked()" ), self.left_button_clicked ) self.connect( up_button, SIGNAL( "clicked()" ), self.up_button_clicked ) self.connect( down_button, SIGNAL( "clicked()" ), self.down_button_clicked ) self.connect( right_button, SIGNAL( "clicked()" ), self.right_button_clicked ) self.color_selection_combo_box = QComboBox( self ) # Method addItem() adds a single item to a QComboBox. self.color_selection_combo_box.addItem( "red" ) # With the addItems() method we can add a tuple of items to # a QComboBox. self.color_selection_combo_box.addItems( ( "orange", "yellow", "blue", "magenta", "cyan", "pink", "lightgray" ) ) self.connect( self.color_selection_combo_box, SIGNAL( "currentIndexChanged(int)" ), self.color_selection_made ) # We use the term "Operations Panel" to describe the # layout into which buttons and the combo box will be # inserted. operations_panel = QHBoxLayout() # We use the addStretch() method before and after we # add the widgets to the operations panel. This way the # widgets will be centered on the panel. operations_panel.addStretch( 1 ) operations_panel.addWidget( left_button ) operations_panel.addWidget( up_button ) operations_panel.addWidget( down_button ) operations_panel.addWidget( right_button ) operations_panel.addWidget( self.color_selection_combo_box ) operations_panel.addStretch( 1 ) main_vertical_layout = QVBoxLayout() main_vertical_layout.addStretch( 1 ) main_vertical_layout.addLayout( operations_panel ) self.setLayout( main_vertical_layout ) self.current_ball_color = QColor( "red" ) self.ball_center_point_x = self.width() / 2 self.ball_center_point_y = self.height() / 2 def left_button_clicked( self ) : self.ball_center_point_x = self.ball_center_point_x - 3 self.update() def right_button_clicked( self ) : self.ball_center_point_x = self.ball_center_point_x + 3 self.update() def up_button_clicked( self ) : self.ball_center_point_y = self.ball_center_point_y - 3 self.update() def down_button_clicked( self ) : self.ball_center_point_y = self.ball_center_point_y + 3 self.update() def right_button_clicked( self ) : self.ball_center_point_x = self.ball_center_point_x + 3 self.update() def color_selection_made( self, index ) : self.current_ball_color = \ QColor( self.color_selection_combo_box.itemText( index ) ) self.update() def paintEvent( self, event ) : painter = QPainter() painter.begin( self ) painter.setBrush( self.current_ball_color ) # drawEllipse() method is used to fill the ball with the current # 'brush' color. In addition it draws the outline of the ball # with the current 'pen' color which is black. painter.drawEllipse( self.ball_center_point_x - 50, self.ball_center_point_y - 50, 100, 100 ) painter.drawText( 20, 20, "(%d, %d)" % \ ( self.ball_center_point_x, self.ball_center_point_y ) ) painter.end() this_application = QApplication( sys.argv ) application_window = MovingBallWindow() application_window.show() this_application.exec_()