1.11M Members

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()``````
Isn't it about time forums rewarded their contributors?

Earn rewards points for helping others. Gain kudos. Cash out. Get better answers yourself.

It's as simple as contributing editorial or replying to discussions labeled or OP Kudos

You
This is an OP Kudos discussion and contributors may be rewarded
Post: