Improve wxPython OnPaint

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 several circles [0, 2000] every N milliseconds (50 in my example), where at each step these circles can update their location.

The method I wrote works (the circles are smoothed too), but it is rather slow. Is there a way to improve my solution?

+3


source to share


2 answers


I think the main idea for better performance is to draw attention to the memory behind the scenes and then display the result on the screen. You can use BufferedCanvas to accomplish this (example provided at the bottom of the link page).



+1


source


Better explain why you described it as being rather slow. (This code looks like it might be part of a pool table simulation.) Profiling will help you there for a while. I had to add a lot of code to get your code snippet running, but some quick observations: 1) you probably need dc.Clear () in your OnPaint () program, or you will get "traces" of all the previous drawn circles. , you can use Buffered DC for smoother graphics updates by changing

def OnPaint(self, evt):
    self.DrawEntities(wx.PaintDC(self))

      



to

 def OnPaint(self, evt):
     self.DrawEntities(wx.BufferedPaintDC(self))

      

0


source







All Articles