# CurtainsGTK.py (c) Kari Laitinen # http://www.naturalprogramming.com # 2009-02-17 File created. # 2009-02-24 Last modification. # This program displays a 'window' that has curtains. # You can adjust the widths and colors of the curtains. # It seems that in PyGTK color components are set with values ranging # from 0 ... 0xFFFF. This program displays the more usual values # in the range 0 ... 255 ( 0 ... 0xFF ) for the red, green, and blue # color components. # The program shows a widget for setting the alpha value of the # used curtain color. The alpha value usually indicates the opacity # of the color. The setting of the alpha value has, unfortunately, # no effect in this program. # This program demonstrates the use of the following widgets # HScale( Scale ) # SpinButton import gtk class CurtainsApplication : def __init__( self ) : self.application_window = gtk.Window( gtk.WINDOW_TOPLEVEL ) self.application_window.set_title( "Adjust curtains with HScales and SpinButtons" ) self.application_window.connect("destroy", lambda w: gtk.main_quit() ) self.application_window.set_size_request( 600, 550 ) self.drawing_area = gtk.DrawingArea() self.drawing_area.set_size_request( 600, 450 ) self.drawing_area.connect( "expose-event", self.drawing_area_exposed ) # Next we'll create sliders to adjust the widths of the curtains. # Note: 300 is the WINDOW_WIDTH used in the drawing_area_exposed method. # You should change it here if you modify the constant inside the mentioned # method. self.left_curtain_slider = gtk.HScale( gtk.Adjustment( 300 / 4, 10, 300 / 2 + 10, 1 ) ) self.right_curtain_slider = gtk.HScale( gtk.Adjustment( 300 / 4, 10, 300 / 2 + 10, 1 ) ) self.right_curtain_slider.set_inverted( True ) self.left_curtain_slider.connect( "value-changed", self.slider_value_changed ) self.right_curtain_slider.connect( "value-changed", self.slider_value_changed ) # Let's prohibit the drawing of numerical SpinButton values self.left_curtain_slider.set_draw_value( False ) self.right_curtain_slider.set_draw_value( False ) curtain_adjustments_panel = gtk.HBox( False, 0 ) curtain_adjustments_panel.pack_start( self.left_curtain_slider ) curtain_adjustments_panel.pack_start( self.right_curtain_slider ) self.red_color_spinner = gtk.SpinButton( gtk.Adjustment( 255, 0, 255, 5 ), 1, 0 ) self.green_color_spinner = gtk.SpinButton( gtk.Adjustment( 50, 0, 255, 5 ), 1, 0 ) self.blue_color_spinner = gtk.SpinButton( gtk.Adjustment( 255, 0, 255, 5 ), 1, 0 ) self.alpha_value_spinner = gtk.SpinButton( gtk.Adjustment( 240, 0, 255, 5 ), 1, 0 ) self.red_color_spinner.connect( "value-changed", self.color_adjustment_made ) self.green_color_spinner.connect( "value-changed", self.color_adjustment_made ) self.blue_color_spinner.connect( "value-changed", self.color_adjustment_made ) self.alpha_value_spinner.connect( "value-changed", self.color_adjustment_made ) self.red_color_label = gtk.Label( "RED:" ) self.green_color_label = gtk.Label( "GREEN:" ) self.blue_color_label = gtk.Label( "BLUE:" ) self.alpha_value_label = gtk.Label( "ALPHA:" ) color_adjustments_panel = gtk.HBox( False, 0 ) color_adjustments_panel.pack_start( self.red_color_label ) color_adjustments_panel.pack_start( self.red_color_spinner ) color_adjustments_panel.pack_start( self.green_color_label ) color_adjustments_panel.pack_start( self.green_color_spinner ) color_adjustments_panel.pack_start( self.blue_color_label ) color_adjustments_panel.pack_start( self.blue_color_spinner ) color_adjustments_panel.pack_start( self.alpha_value_label ) color_adjustments_panel.pack_start( self.alpha_value_spinner ) operations_panel = gtk.VBox( False, 0 ) operations_panel.pack_start( curtain_adjustments_panel ) operations_panel.pack_start( color_adjustments_panel ) 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 ) self.application_window.show_all() def color_adjustment_made( self, widget, data=None ) : self.drawing_area.queue_draw() def slider_value_changed( self, widget, data=None ) : 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 # The word WINDOW in the following names refers to the 'window' # that is drawn onto the screen by this program. WINDOW_UPPER_LEFT_CORNER_X = 150 WINDOW_UPPER_LEFT_CORNER_Y = 50 WINDOW_WIDTH = 300 WINDOW_HEIGHT = 350 graphics_context.foreground = \ this_drawable.get_colormap().alloc_color( "blue" ) # sky color this_drawable.draw_rectangle( graphics_context, True, WINDOW_UPPER_LEFT_CORNER_X, WINDOW_UPPER_LEFT_CORNER_Y, WINDOW_WIDTH, WINDOW_HEIGHT / 2 ) graphics_context.foreground = \ this_drawable.get_colormap().alloc_color( "green" ) # lawn color this_drawable.draw_rectangle( graphics_context, True, WINDOW_UPPER_LEFT_CORNER_X, WINDOW_UPPER_LEFT_CORNER_Y + WINDOW_HEIGHT / 2, WINDOW_WIDTH, WINDOW_HEIGHT / 2 ) graphics_context.foreground = \ this_drawable.get_colormap().alloc_color( "yellow" ) # sun color this_drawable.draw_arc( graphics_context, True, WINDOW_UPPER_LEFT_CORNER_X + 50, WINDOW_UPPER_LEFT_CORNER_Y + 50, 50, 50, 0, 360*64 ) graphics_context.foreground = \ this_drawable.get_colormap().alloc_color( "black" ) # frame color this_drawable.draw_rectangle( graphics_context, False, WINDOW_UPPER_LEFT_CORNER_X, WINDOW_UPPER_LEFT_CORNER_Y, WINDOW_WIDTH, WINDOW_HEIGHT) this_drawable.draw_line( graphics_context, WINDOW_UPPER_LEFT_CORNER_X - 10, WINDOW_UPPER_LEFT_CORNER_Y - 10, WINDOW_UPPER_LEFT_CORNER_X + WINDOW_WIDTH + 10, WINDOW_UPPER_LEFT_CORNER_Y - 10 ) # Next we'll read the spinner values and set the current curtains color. graphics_context.foreground = this_drawable.get_colormap().alloc_color( int( self.red_color_spinner.get_value() ) * 0xFF, int( self.green_color_spinner.get_value() ) * 0xFF, int( self.blue_color_spinner.get_value() ) * 0xFF ) this_drawable.draw_rectangle( graphics_context, True, WINDOW_UPPER_LEFT_CORNER_X - 10, WINDOW_UPPER_LEFT_CORNER_Y - 10, int( self.left_curtain_slider.get_value() ), WINDOW_HEIGHT + 20 ) this_drawable.draw_rectangle( graphics_context, True, WINDOW_UPPER_LEFT_CORNER_X + WINDOW_WIDTH + 10 - int( self.right_curtain_slider.get_value() ), WINDOW_UPPER_LEFT_CORNER_Y - 10, int( self.right_curtain_slider.get_value() ), WINDOW_HEIGHT + 20 ) # Finally, we'll set the foreground colors in the gtk.Label objects # so that the text color indicates the selected red, green, or blue # component of the current curtain color. self.red_color_label.modify_fg( gtk.STATE_NORMAL, this_drawable.get_colormap().alloc_color( int( self.red_color_spinner.get_value() ) * 0xFF, 0, 0 ) ) self.green_color_label.modify_fg( gtk.STATE_NORMAL, this_drawable.get_colormap().alloc_color( 0, int( self.green_color_spinner.get_value() ) * 0xFF, 0 ) ) self.blue_color_label.modify_fg( gtk.STATE_NORMAL, this_drawable.get_colormap().alloc_color( 0, 0, int( self.blue_color_spinner.get_value() ) * 0xFF ) ) def run( self ) : gtk.main() if __name__ == "__main__": this_application = CurtainsApplication() this_application.run()