Hi all,

This is the beginning of a hopefully useful project: to create a Canvas widget for wxPython that will mimic or improve on the Tkinter Canvas widget. Those of you who have coded with both will probably know what I'm talking about. With Tkinter, you can plop items like ovals, rectangles, etc. on the canvas and then keep track of them with id #s: check for collisions, move them, etc. With wxPython, you get a blank Panel. Yea.

So, here's the beginnings of an attempt to create a wxPython Canvas widget. The Canvas keeps a list of IDS of items and draws them as needed; that list contains direct references rather than ID #s.

If there are any structural problems or whatnot, let me know. Currently, the only thing you can create are ovals. But the output was so satisfying, I had to share... :)


import wx

class CanvasItem(object):
    """A virtual base class.  All child classes need to define self.DrawMethod
and set self.type"""

    def __init__(self, parent, bbox, pen, brush):
        self.parent = parent
        self.bbox = bbox
        self.pen = pen
        self.brush = brush

    def draw(self, DC):
        self.DrawMethod(DC, self.bbox)

class Oval(CanvasItem):

    def __init__(self, parent, bbox, pen, brush):
        CanvasItem.__init__(self, parent, bbox, pen, brush)

    def DrawMethod(self, dc, bbox):
        dc.DrawEllipse(bbox.x, bbox.y, bbox.width, bbox.height)
class Canvas(wx.Panel):

    # Store IDS in order by leftmost edge for more efficient collision detections.
    def __init__(self, parent=None,id=wx.ID_ANY, pos = wx.DefaultPosition,
        self.IDS = []
        self.Bind(wx.EVT_PAINT, self.OnPaint)

    def OnPaint(self,event):
        DC = wx.PaintDC(self)
        for ID in self.IDS:

    def create_oval(self, x0,y0,x1,y1,outline="black", fill="", stipple="",
        print "Creating oval:", x0,y0,x1,y1,outline,fill,stipple,width,style
        pen = wx.Pen(outline.upper(),width,style)
        if not fill:
            fill = "BLACK"
        elif stipple == "":
            fill = outline.upper()
            style = wx.SOLID

            fill = outline.upper()
            style = wx.CROSS_HATCH  # need to fix this with bitmap stippling?!
        brush = wx.Brush(fill,style)
        self.IDS.append(Oval(self, wx.Rect(x0,y0,x1,y1), pen, brush))

app = wx.PySimpleApp(0)
app.frame = wx.Frame(parent=None, id=-1, size=(300,400))
app.frame.canvas = Canvas(parent=app.frame)
for i in range(10,300,10):

vegaseat commented: nice alternative +7

so cool!!

Be a part of the DaniWeb community

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