1

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... :)

Jeff

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):
        DC.SetPen(self.pen)
        DC.SetBrush(self.brush)
        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,
                 size=wx.DefaultSize,style=wx.TAB_TRAVERSAL,name="panel"):
        wx.Panel.__init__(self,parent,id,pos,size,style,name)
        self.IDS = []
        self.Bind(wx.EVT_PAINT, self.OnPaint)

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

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

        else:
            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):
    app.frame.canvas.create_oval(10,10,i,i)

app.frame.Show()
app.MainLoop()
Votes + Comments
nice alternative
2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by BJSH
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.