# MovingBall00GTK.py (c) Kari Laitinen # http://www.naturalprogramming.com # The development of this program has been sponsored by Nokia Multimedia. # 2006-09-25 File created. # 2006-09-25 Last modification. # This program is an object-oriented version of program # MovingBallGTK.py. # This one uses a class named Ball that is declared in its # own source program file. import gtk from BallGTK import Ball class MovingBall : def __init__( self ) : self.application_window = gtk.Window( gtk.WINDOW_TOPLEVEL ) self.application_window.set_title( "Move the Ball with Buttons" ) self.application_window.connect("destroy", lambda w: gtk.main_quit() ) self.application_window.set_size_request( 600, 500 ) self.drawing_area = gtk.DrawingArea() self.drawing_area.set_size_request( 600, 450 ) self.drawing_area.connect( "expose-event", self.drawing_area_exposed ) left_button = gtk.Button( " < " ) up_button = gtk.Button( " Up " ) down_button = gtk.Button( " Down " ) right_button = gtk.Button( " > " ) left_button.connect( "clicked", self.left_button_clicked ) up_button.connect( "clicked", self.up_button_clicked ) down_button.connect( "clicked", self.down_button_clicked ) right_button.connect( "clicked", self.right_button_clicked ) operations_panel = gtk.HBox( False, 0 ) operations_panel.pack_start( left_button ) operations_panel.pack_start( up_button ) operations_panel.pack_start( down_button ) operations_panel.pack_start( right_button ) color_selection_menu = gtk.combo_box_entry_new_text() color_selection_menu.append_text( "red" ) color_selection_menu.append_text( "orange" ) color_selection_menu.append_text( "yellow" ) color_selection_menu.append_text( "green" ) color_selection_menu.append_text( "blue" ) color_selection_menu.append_text( "magenta" ) color_selection_menu.append_text( "cyan" ) color_selection_menu.append_text( "pink" ) color_selection_menu.append_text( "lightGray" ) operations_panel.pack_start( color_selection_menu ) color_selection_menu.set_active( 0 ) color_selection_menu.child.connect( "changed", self.color_selection_menu_used ) main_vertical_box = gtk.VBox( False, 0 ) main_vertical_box.pack_start( self.drawing_area, False, False, 3 ) main_vertical_box.pack_start( operations_panel, False, False, 3 ) self.application_window.add( main_vertical_box ) window_width, window_height = self.application_window.get_size() self.ball_on_screen = Ball( window_width / 2 - 40, window_height / 2 - 40, "red" ) self.drawing_area.show() operations_panel.show() left_button.show() up_button.show() down_button.show() right_button.show() color_selection_menu.show() main_vertical_box.show() self.application_window.show() def left_button_clicked( self, widget, data=None ) : self.ball_on_screen.move_left() self.drawing_area.queue_draw() def up_button_clicked( self, widget, data=None ) : self.ball_on_screen.move_up() self.drawing_area.queue_draw() def down_button_clicked( self, widget, data=None ) : self.ball_on_screen.move_down() self.drawing_area.queue_draw() def right_button_clicked( self, widget, data=None ) : self.ball_on_screen.move_right() self.drawing_area.queue_draw() def color_selection_menu_used( self, menu_selection ) : self.ball_on_screen.set_color( menu_selection.get_text() ) self.drawing_area.queue_draw() def drawing_area_exposed( self, area, event ) : self.ball_on_screen.draw( self.drawing_area ) graphics_context = self.drawing_area.get_style().fg_gc[gtk.STATE_NORMAL] this_drawable = self.drawing_area.window graphics_context.foreground = \ this_drawable.get_colormap().alloc_color( "black" ) text_to_show = self.drawing_area.create_pango_layout( "" ) text_to_show.set_text( "(%d, %d)" % \ ( self.ball_on_screen.get_ball_position_x(), self.ball_on_screen.get_ball_position_y() ) ) this_drawable.draw_layout( graphics_context, 20, 20, text_to_show ) return True def run( self ) : gtk.main() if __name__ == "__main__": this_application = MovingBall() this_application.run()