HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Fast draw wxPython OnPaint

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
fastdrawwxpythononpaint

Problem

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?

Solution

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!

Code Snippets

def my_func( ... ):
    """
    Describe what your function does and
    it's arguments in this docstring.
    """
    ...

Context

StackExchange Code Review Q#67834, answer score: 4

Revisions (0)

No revisions yet.