# CurtainsQt.py (c) Copyright Kari Laitinen # http://www.naturalprogramming.com # 2010-09-16 File created. # 2013-10-14 Last modification (Some names changed.) # This program displays a 'window' that has curtains. # You can adjust the widths and colors of the curtains. import sys from PyQt4.QtCore import * from PyQt4.QtGui import * class CurtainsWindow( QWidget ) : def __init__( self, parent = None ) : QWidget.__init__( self, parent ) self.setWindowTitle( "ADJUST CURTAIN COLORS AND WIDTHS" ) self.setGeometry( 100, 100, 600, 550 ) # Next we'll create sliders to adjust the widths of the curtains. # Note: 300 is the WINDOW_WIDTH used in the painting method. # You should change it here if you modify the constant inside the mentioned # method. self.left_curtain_slider = QSlider( Qt.Horizontal ) self.left_curtain_slider.setRange( 10, 300 / 2 + 10 ) self.left_curtain_slider.setValue( 300 / 4 ) self.right_curtain_slider = QSlider( Qt.Horizontal ) self.right_curtain_slider.setRange( 10, 300 / 2 + 10 ) self.right_curtain_slider.setValue( 300 / 4 ) self.right_curtain_slider.setInvertedAppearance( True ) self.connect( self.left_curtain_slider, SIGNAL( "valueChanged(int)" ), self.slider_value_changed ) self.connect( self.right_curtain_slider, SIGNAL( "valueChanged(int)" ), self.slider_value_changed ) curtain_adjustments_panel = QHBoxLayout() curtain_adjustments_panel.addWidget( self.left_curtain_slider ) curtain_adjustments_panel.addWidget( self.right_curtain_slider ) self.red_color_spin_box = QSpinBox() self.green_color_spin_box = QSpinBox() self.blue_color_spin_box = QSpinBox() self.alpha_value_spin_box = QSpinBox() self.red_color_spin_box.setRange( 0, 255 ) self.red_color_spin_box.setSingleStep( 5 ) self.red_color_spin_box.setValue( 255 ) self.green_color_spin_box.setRange( 0, 255 ) self.green_color_spin_box.setSingleStep( 5 ) self.green_color_spin_box.setValue( 50 ) self.blue_color_spin_box.setRange( 0, 255 ) self.blue_color_spin_box.setSingleStep( 5 ) self.blue_color_spin_box.setValue( 255 ) self.alpha_value_spin_box.setRange( 0, 255 ) self.alpha_value_spin_box.setSingleStep( 5 ) self.alpha_value_spin_box.setValue( 240 ) self.connect( self.red_color_spin_box, SIGNAL( "valueChanged(int)" ), self.color_adjustment_made ) self.connect( self.green_color_spin_box, SIGNAL( "valueChanged(int)" ), self.color_adjustment_made ) self.connect( self.blue_color_spin_box, SIGNAL( "valueChanged(int)" ), self.color_adjustment_made ) self.connect( self.alpha_value_spin_box, SIGNAL( "valueChanged(int)" ), self.color_adjustment_made ) self.red_color_label = QLabel( "RED:" ) self.green_color_label = QLabel( "GREEN:" ) self.blue_color_label = QLabel( "BLUE:" ) self.alpha_value_label = QLabel( "ALPHA:" ) color_adjustments_panel = QHBoxLayout() # We use the addStretch() method to center the widgets on the panel. color_adjustments_panel.addStretch( 1 ) color_adjustments_panel.addWidget( self.red_color_label ) color_adjustments_panel.addWidget( self.red_color_spin_box ) color_adjustments_panel.addWidget( self.green_color_label ) color_adjustments_panel.addWidget( self.green_color_spin_box ) color_adjustments_panel.addWidget( self.blue_color_label ) color_adjustments_panel.addWidget( self.blue_color_spin_box ) color_adjustments_panel.addWidget( self.alpha_value_label ) color_adjustments_panel.addWidget( self.alpha_value_spin_box ) color_adjustments_panel.addStretch( 1 ) main_vertical_layout = QVBoxLayout() main_vertical_layout.addStretch( 1 ) # should the two panels be attached into a separate vertical box +???? main_vertical_layout.addLayout( curtain_adjustments_panel ) main_vertical_layout.addLayout( color_adjustments_panel ) self.setLayout( main_vertical_layout ) def color_selection_menu_used( self, index ) : self.current_ball_color = \ QColor( self.color_selection_menu.itemText( index ) ) self.update() def color_adjustment_made( self ) : self.update() def slider_value_changed( self ) : self.update() def paintEvent( self, event ) : painter = QPainter() painter.begin( self ) # 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 painter.fillRect( WINDOW_UPPER_LEFT_CORNER_X, WINDOW_UPPER_LEFT_CORNER_Y, WINDOW_WIDTH, WINDOW_HEIGHT / 2, Qt.blue ) # sky painter.fillRect( WINDOW_UPPER_LEFT_CORNER_X, WINDOW_UPPER_LEFT_CORNER_Y + WINDOW_HEIGHT / 2, WINDOW_WIDTH, WINDOW_HEIGHT / 2, Qt.green) # lawn painter.setBrush( Qt.yellow ) # sun color painter.drawEllipse( WINDOW_UPPER_LEFT_CORNER_X + 50, WINDOW_UPPER_LEFT_CORNER_Y + 50, 50, 50 ) painter.setBrush( Qt.NoBrush ) painter.drawRect( WINDOW_UPPER_LEFT_CORNER_X, WINDOW_UPPER_LEFT_CORNER_Y, WINDOW_WIDTH, WINDOW_HEIGHT ) painter.drawLine( 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 spin box values and set the current curtains color. current_curtains_color = QColor( self.red_color_spin_box.value(), self.green_color_spin_box.value(), self.blue_color_spin_box.value(), self.alpha_value_spin_box.value() ) painter.fillRect( WINDOW_UPPER_LEFT_CORNER_X - 10, WINDOW_UPPER_LEFT_CORNER_Y - 10, self.left_curtain_slider.value(), WINDOW_HEIGHT + 20, current_curtains_color ) painter.fillRect( WINDOW_UPPER_LEFT_CORNER_X + WINDOW_WIDTH + 10 - self.right_curtain_slider.value(), WINDOW_UPPER_LEFT_CORNER_Y - 10, self.right_curtain_slider.value(), WINDOW_HEIGHT + 20, current_curtains_color ) # Finally we'll modify the colors of the texts inside QLabel objects # to correspond to selected red, green, and blue values. # Each widget in Qt (e.g. each Qlabel object) contains a color palette # that dictates which colors are used in that particular widget. # Now we take the current palettes inside QLabel objects, modify them, # and put them back after modification. red_color_label_palette = self.red_color_label.palette() green_color_label_palette = self.green_color_label.palette() blue_color_label_palette = self.blue_color_label.palette() red_color_label_palette.setColor( QPalette.WindowText, QColor( self.red_color_spin_box.value(), 0, 0 ) ) green_color_label_palette.setColor( QPalette.WindowText, QColor( 0, self.green_color_spin_box.value(), 0 ) ) blue_color_label_palette.setColor( QPalette.WindowText, QColor( 0, 0, self.blue_color_spin_box.value() ) ) self.red_color_label.setPalette( red_color_label_palette ) self.green_color_label.setPalette( green_color_label_palette ) self.blue_color_label.setPalette( blue_color_label_palette ) painter.end() # And here is the "main" program: this_application = QApplication( sys.argv ) application_window = CurtainsWindow() application_window.show() this_application.exec_()