943,708 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 5696
  • Python RSS
Feb 27th, 2008
1

wxPython Canvas widget -- RFC

Expand Post »
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

Python Syntax (Toggle Plain Text)
  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()
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Feb 27th, 2008
0

Re: wxPython Canvas widget -- RFC

so cool!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BJSH is offline Offline
1 posts
since Feb 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Extract lines with values in range
Next Thread in Python Forum Timeline: overflow





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC