I have the following wx.Window
:
class SketchWindow(wx.Window):
def __init__(self, parent):
wx.Window.__init__(self, parent, -1)
self.SetBackgroundColour('White')
# Window event binding
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_IDLE, self.OnIdle)
# run
self.Run()
def OnPaint(self, evt):
self.DrawEntities(wx.PaintDC(self))
def DrawEntities(self, dc):
dc.SetPen(wx.Pen('Black', 1, wx.SOLID))
dc.SetBrush(wx.Brush('Green', wx.SOLID))
# draw all
for e in self.entities:
x, y = e.location
dc.DrawCircle(x, y, 4)
def OnIdle(self, event):
self.Refresh(False)
def Run(self):
# update self.entities ...
# call this method again later
wx.CallLater(50, self.Run)
I need to draw on my window a number of circles [0, 2000] every N milliseconds (50 in my example), where at each step these circles may update their location.
The method I wrote works (the circles are anti-aliased too) but it's quite slow. Is there a way to improve my solution?
-
\$\begingroup\$ Why do you need to update so quickly? That's way faster than the eye can see. \$\endgroup\$Mike Driscoll– Mike Driscoll2014年10月24日 16:22:57 +00:00Commented Oct 24, 2014 at 16:22
-
\$\begingroup\$ @MikeDriscoll Actually if I don't keep the delay so low the animation results very slow (I can't say way). Also on my computer I need 0.005 seconds to update entities and about 0.05 seconds to DrawEntities, therefore 50 milliseconds is the lower bound (I can't go faster...). \$\endgroup\$Nick– Nick2014年10月24日 16:29:56 +00:00Commented Oct 24, 2014 at 16:29
-
\$\begingroup\$ I can't find anything about speed gains for that widget. Drawing 2000 circles every 50 milliseconds sounds pretty extreme though. I don't think anyone will be able to see the drawing taking place at that speed. But you might try floatcanvas (wxpython.org/Phoenix/docs/html/lib.floatcanvas.html) or possibly wxPython's Cairo interface. \$\endgroup\$Mike Driscoll– Mike Driscoll2014年10月24日 18:01:05 +00:00Commented Oct 24, 2014 at 18:01
-
\$\begingroup\$ Have you tried using wx.BufferedPaintDC instead of wx.PaintDC? \$\endgroup\$RufusVS– RufusVS2014年12月23日 19:30:36 +00:00Commented Dec 23, 2014 at 19:30
1 Answer 1
Generally, the Python style is to indent by one tab, or four spaces. Functions and variables should be named in snake_case
, and classes should be in PascalCase
. See PEP8 for more style information.
You should also use docstrings to describe what your functions do. Here's an example of how docstrings are used.
def my_func( ... ):
"""
Describe what your function does and
it's arguments in this docstring.
"""
...
Other than that, your code looks pretty nice! Good work!