In this experiment we call the paint event and establish a device context to draw a rectangle and fill it with the color set by the brush. Actually, we will draw 99 random sized and random colored rectangles with a small time delay. Don't worry, it will be faster than the "99 bottles of beer on the wall" song!

This kind of frivolity can be expanded to lines, polygons, dots and circles. Something my little nephew Carlos Vegaseat enjoys watching!

# experimenting with wxPython's DrawRectangle()
# the rectangle is filled with the brush color
# tested with Python24 and wxPython26     vegaseat    19oct2005

import random
import wx
import time

class MyPanel(wx.Panel):
    """ class MyPanel creates a panel to draw on, inherits wx.Panel """
    def __init__(self, parent, id):
        # create a panel
        wx.Panel.__init__(self, parent, id)
        # start the paint event for DrawRectangle() and FloodFill()
        self.Bind(wx.EVT_PAINT, self.OnPaint)
    def OnPaint(self, evt):
        """set up the device context (DC) for painting"""
        self.dc = wx.PaintDC(self)
        # draw a few colorful rectangles ...
        for k in range(100):
            # set random RGB color for brush
            r = random.randrange(256)
            g = random.randrange(256)
            b = random.randrange(256)
            self.dc.SetBrush(wx.Brush((r, g, b), wx.SOLID))
            # set random x, y, w, h for rectangle
            w = random.randint(10, width1/2)
            h = random.randint(10, height1/2)
            x = random.randint(0, width1 - w)
            y = random.randint(0, height1 - h)
            self.dc.DrawRectangle(x, y, w, h)
            time.sleep(0.2)  # delay
            str1 = "Drawing %d Rectangles ..." % (99 - k)
        # free up the device context now
        del self.dc
height1 = 350
width1 = 400

app = wx.PySimpleApp()
# create a window/frame, no parent, -1 is default ID
frame = wx.Frame(None, -1, "Drawing 99 Rectangles ...", size = (width1, height1))
# call the derived class, -1 is default ID
# show the frame
# start the event loop