Refreshing wx.GraphicsContext

This is based on the code from GraphicContext demo.
def OnPaint(self, event=None):
        """"""
    dc = wx.PaintDC(self)
    self.gc = wx.GraphicsContext.Create(dc)
    self.gc.Draw....
    self.gc.Draw...
    self.gc.Draw...
    event.Skip()

The drawing operation is getting changed in mouse (move,down) events.
How and where do I refresh the Panel/DC
to update the changes?

I tried using self.Refresh() just before ending the OnMouseMotion
function but it produces flicker. self.Update() doesn't do anything.

Prashant

Python 2.6.2
wxPython 2.8.10.1
XP 32

Refresh tells the system that the window (or some sub-rectangle of it) needs to be redrawn and to please schedule a paint event for it. Update says process any pending paint events right now, so typically if you're going to use Update then there will have been a Refresh sometime before it.

On Windows flicker is usually caused by two related things. The first is clearing the window before redrawing it. The second is doing more than one drawing operation that takes long enough that the video driver will flush the intermediate state to the display, resulting in the user being able to see partial updates before the drawing is completed. (As you can see the first is just a particular case of the second, but I mention it separately because it has a different solution.)

If you're going to be drawing the whole window anyway, then you can get the system to skip clearing it first and that will take care of the first kind of flicker. Traditionally that was done by catching the EVT_ERASE_BACKGROUND event and doing nothing in the handler, but now you can do it by calling the window's SetBackgroundStyle method and passing wx.BG_STYLE_CUSTOM.

If that doesn't help enough in your case then take a look at how you are drawing and reorganize it to only draw the portion of the window that has changed, or that the system is telling you needs to be redrawn (use GetUpdateRegion to find out the rectangles that need to be refreshed.)

If that still isn't enough flicker reduction then you'll want to use the double buffered technique. In a nutshell you draw everything to a bitmap first and then the only actual drawing operation that goes to the screen is drawing that bitmap. There are lots of examples of this technique in he archives of this list, in the demo and on the wiki.

···

On 12/9/09 12:42 AM, King wrote:

This is based on the code from GraphicContext demo.
def OnPaint(self, event=None):
         """"""
     dc = wx.PaintDC(self)
     self.gc = wx.GraphicsContext.Create(dc)
     self.gc.Draw....
     self.gc.Draw...
     event.Skip()

The drawing operation is getting changed in mouse (move,down) events.
How and where do I refresh the Panel/DC
to update the changes?

I tried using self.Refresh() just before ending the OnMouseMotion
function but it produces flicker. self.Update() doesn't do anything.

--
Robin Dunn
Software Craftsman