wxPython Canvas widget -- RFC

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

wxPython Canvas widget -- RFC

 
1
  #1
Feb 27th, 2008
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

  1. import wx
  2.  
  3. class CanvasItem(object):
  4. """A virtual base class. All child classes need to define self.DrawMethod
  5. and set self.type"""
  6.  
  7. def __init__(self, parent, bbox, pen, brush):
  8. self.parent = parent
  9. self.bbox = bbox
  10. self.pen = pen
  11. self.brush = brush
  12.  
  13. def draw(self, DC):
  14. DC.SetPen(self.pen)
  15. DC.SetBrush(self.brush)
  16. self.DrawMethod(DC, self.bbox)
  17.  
  18. class Oval(CanvasItem):
  19.  
  20. def __init__(self, parent, bbox, pen, brush):
  21. CanvasItem.__init__(self, parent, bbox, pen, brush)
  22.  
  23. def DrawMethod(self, dc, bbox):
  24. dc.DrawEllipse(bbox.x, bbox.y, bbox.width, bbox.height)
  25.  
  26.  
  27. class Canvas(wx.Panel):
  28.  
  29. # Store IDS in order by leftmost edge for more efficient collision detections.
  30.  
  31. def __init__(self, parent=None,id=wx.ID_ANY, pos = wx.DefaultPosition,
  32. size=wx.DefaultSize,style=wx.TAB_TRAVERSAL,name="panel"):
  33. wx.Panel.__init__(self,parent,id,pos,size,style,name)
  34. self.IDS = []
  35. self.Bind(wx.EVT_PAINT, self.OnPaint)
  36.  
  37. def OnPaint(self,event):
  38. DC = wx.PaintDC(self)
  39. for ID in self.IDS:
  40. ID.draw(DC)
  41.  
  42. def create_oval(self, x0,y0,x1,y1,outline="black", fill="", stipple="",
  43. width=1,style=wx.SOLID):
  44. print "Creating oval:", x0,y0,x1,y1,outline,fill,stipple,width,style
  45. pen = wx.Pen(outline.upper(),width,style)
  46. if not fill:
  47. fill = "BLACK"
  48. style=wx.TRANSPARENT
  49. elif stipple == "":
  50. fill = outline.upper()
  51. style = wx.SOLID
  52.  
  53. else:
  54. fill = outline.upper()
  55. style = wx.CROSS_HATCH # need to fix this with bitmap stippling?!
  56. brush = wx.Brush(fill,style)
  57. self.IDS.append(Oval(self, wx.Rect(x0,y0,x1,y1), pen, brush))
  58.  
  59. app = wx.PySimpleApp(0)
  60. app.frame = wx.Frame(parent=None, id=-1, size=(300,400))
  61. app.frame.canvas = Canvas(parent=app.frame)
  62. for i in range(10,300,10):
  63. app.frame.canvas.create_oval(10,10,i,i)
  64.  
  65. app.frame.Show()
  66. app.MainLoop()
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1
Reputation: BJSH is an unknown quantity at this point 
Solved Threads: 0
BJSH BJSH is offline Offline
Newbie Poster

Re: wxPython Canvas widget -- RFC

 
0
  #2
Feb 27th, 2008
so cool!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC