wxPython DrawRectangle Experiment

vegaseat 0 Tallied Votes 820 Views Share

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)
        self.SetBackgroundColour("white")
        
        # 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)
        self.dc.Clear()
        self.dc.BeginDrawing()
        self.dc.SetPen(wx.Pen("BLACK",1))
        # 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)
            frame.SetTitle(str1)
        self.dc.EndDrawing()
        # 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
MyPanel(frame,-1)
# show the frame
frame.Show(True)
# start the event loop
app.MainLoop()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.