Start New Discussion within our Software Development Community

A colorful look at the PySide GUI toolkit and its QPainter class and methods. Well commented and demonstrated with the drawing of a number of rectangles and the different ways to apply colors.

explore the PySide GUI toolkit to draw rectangles in different colors
there are a number of ways colors can be specified

fill colors are set with the brush
perimeter colors are set with the pen
QColor can be given a transparency value

(PySide is the official LGPL-licensed version of PyQT)
for Python33 you can use the Windows self-extracting installer
(PyQT483 equivalent) from:

for Qpainter methods see:

tested with Python27 and Python33 by  vegaseat  14jan2013

from PySide.QtCore import *
from PySide.QtGui import *

class MyWindow(QWidget):

    def __init__(self):
        # setGeometry(x_pos, y_pos, width, height)
        # upper left corner coordinates (x_pos, y_pos)
        self.setGeometry(300, 300, 370, 100)
        self.setWindowTitle('Colors set with brush and pen')

    def paintEvent(self, e):
        the method paintEvent() is called automatically
        the QPainter class does all the low-level drawing
        coded between its methods begin() and end()
        qp = QPainter()

    def drawRectangles(self, qp):
        '''use QPainter (instance qp) methods to do drawings'''
        # there are several different ways to reference colors
        # use HTML style color string #RRGGBB with values 00 to FF
        black = "#000000"
        # QPen(color, width, style)

        # use QColor(r, g, b) with values 0 to 255
        qp.setBrush(QColor(255, 0, 0))
        # drawRect(int x, int y, int width, int height)
        # upper left corner coordinates (x, y)
        qp.drawRect(10, 15, 90, 60)

        # there are some preset named colors
        qp.drawRect(160, 25, 90, 60)

        # this rectangle will overlap the previous one
        # you can give it some transparency alpha 0 to 255
        # QColor(int r, int g, int b, int alpha=255)
        qp.setBrush(QColor(0, 0, 255, 100))
        qp.drawRect(130, 15, 90, 60)

        # some colors can be given as strings
        qp.drawRect(265, 25, 90, 60)

app = QApplication([])
win = MyWindow()
# run the application event loop
The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.