# RectangleGTK.py (c) Kari Laitinen # http://www.naturalprogramming.com # The development of this program has been sponsored by Nokia Multimedia. # 2006-09-14 File created. # 2006-09-19 Last modification. import gtk class RectangleApplication : def __init__( self ) : self.application_window = gtk.Window( gtk.WINDOW_TOPLEVEL ) self.application_window.set_title( "ADJUST RECTANGLE SIZE AND COLOR" ) self.application_window.connect("destroy", lambda w: gtk.main_quit() ) self.application_window.set_size_request( 600, 400 ) self.drawing_area = gtk.DrawingArea() self.drawing_area.set_size_request( 580, 360 ) self.drawing_area.connect( "expose-event", self.drawing_area_exposed ) self.rectangle_color = "cyan" window_width, window_height = self.application_window.get_size() self.rectangle_center_position_x = window_width / 2 self.rectangle_center_position_y = window_height / 2 self.rectangle_width = window_width / 4 self.rectangle_height = window_height / 4 # The first RadioButton object is created with parameter None. # The rest of the RadioButtons are created with parameter # self.cyan_button. This makes these buttons to belong to the same # group of buttons as self.cyan_button. self.cyan_button = gtk.RadioButton( None, "cyan" ) self.magenta_button = gtk.RadioButton( self.cyan_button, "magenta" ) self.green_button = gtk.RadioButton( self.cyan_button, "green" ) self.lightGray_button = gtk.RadioButton( self.cyan_button, "lightGray" ) self.cyan_button.set_active( True ) self.cyan_button.connect( "clicked", self.color_selection_made ) self.magenta_button.connect( "clicked", self.color_selection_made ) self.green_button.connect( "clicked", self.color_selection_made ) self.lightGray_button.connect( "clicked", self.color_selection_made ) height_adjustment = gtk.Adjustment( self.rectangle_height, 0, window_height, 2, 10 ) height_adjustment.connect( "value_changed", self.rectangle_height_changed ) height_scrollbar = gtk.VScrollbar( height_adjustment ) width_adjustment = gtk.Adjustment( self.rectangle_width, 0, window_width, 2, 10 ) width_scrollbar = gtk.HScrollbar( width_adjustment ) width_adjustment.connect( "value_changed", self.rectangle_width_changed ) main_vertical_box = gtk.VBox( False, 0 ) box_for_drawing_area_and_height_scrollbar = gtk.HBox( False, 0 ) box_for_width_scrollbar_and_buttons = gtk.HBox( False, 0 ) box_for_drawing_area_and_height_scrollbar.pack_start( self.drawing_area ) box_for_drawing_area_and_height_scrollbar.pack_start( height_scrollbar ) box_for_width_scrollbar_and_buttons.pack_start( width_scrollbar ) box_for_width_scrollbar_and_buttons.pack_start( self.cyan_button ) box_for_width_scrollbar_and_buttons.pack_start( self.magenta_button ) box_for_width_scrollbar_and_buttons.pack_start( self.green_button ) box_for_width_scrollbar_and_buttons.pack_start( self.lightGray_button ) main_vertical_box.pack_start( box_for_drawing_area_and_height_scrollbar, False, False, 3 ) main_vertical_box.pack_start( box_for_width_scrollbar_and_buttons, False, False, 3 ) self.application_window.add( main_vertical_box ) self.drawing_area.show() self.cyan_button.show() self.magenta_button.show() self.green_button.show() self.lightGray_button.show() height_scrollbar.show() width_scrollbar.show() box_for_drawing_area_and_height_scrollbar.show() box_for_width_scrollbar_and_buttons.show() main_vertical_box.show() self.application_window.show() def color_selection_made( self, widget, data = None ) : if self.cyan_button.get_active() == True : self.rectangle_color = "cyan" elif self.magenta_button.get_active() == True : self.rectangle_color = "magenta" elif self.green_button.get_active() == True : self.rectangle_color = "green" elif self.lightGray_button.get_active() == True : self.rectangle_color = "lightGray" self.drawing_area.queue_draw() def rectangle_width_changed( self, adjustment, data = None ) : self.rectangle_width = int( adjustment.get_value() ) self.drawing_area.queue_draw() def rectangle_height_changed( self, adjustment, data = None ) : self.rectangle_height = int( adjustment.get_value() ) self.drawing_area.queue_draw() def drawing_area_exposed( self, area, event ) : 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( self.rectangle_color ) this_drawable.draw_rectangle( graphics_context, True, self.rectangle_center_position_x - self.rectangle_width / 2, self.rectangle_center_position_y - self.rectangle_height / 2, self.rectangle_width, self.rectangle_height ) graphics_context.foreground = \ this_drawable.get_colormap().alloc_color( "black" ) this_drawable.draw_rectangle( graphics_context, False, self.rectangle_center_position_x - self.rectangle_width / 2, self.rectangle_center_position_y - self.rectangle_height / 2, self.rectangle_width, self.rectangle_height ) text_to_show = self.drawing_area.create_pango_layout( "" ) text_to_show.set_text( "Width: %d Height: %d" % \ ( self.rectangle_width, self.rectangle_height ) ) this_drawable.draw_layout( graphics_context, 20, 20, text_to_show ) return True def run( self ) : gtk.main() if __name__ == "__main__": this_application = RectangleApplication() this_application.run()