4
\$\begingroup\$

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?

200_success
145k22 gold badges190 silver badges478 bronze badges
asked Oct 24, 2014 at 16:05
\$\endgroup\$
4
  • \$\begingroup\$ Why do you need to update so quickly? That's way faster than the eye can see. \$\endgroup\$ Commented 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\$ Commented 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\$ Commented Oct 24, 2014 at 18:01
  • \$\begingroup\$ Have you tried using wx.BufferedPaintDC instead of wx.PaintDC? \$\endgroup\$ Commented Dec 23, 2014 at 19:30

1 Answer 1

4
\$\begingroup\$

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!

answered Jul 5, 2015 at 0:19
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.