Hi Andrew,
ok, i'm sure i'm doing something retarded here; my problem is that after a make a simple drawing in my window, if I then switch to another application that happens to cover my wxapp or simply minimize my wxapp, the drawing disappears. what am i doing wrong here?
Just because you draw into a window doesn't mean that what you draw stays there forevermore! Looking at your code, I'm guessing that you're calling this in response to the user clicking somewhere in the window. That's fine -- but you also need to define an OnPaint() method and tell wxPython to use it via EVT_PAINT, so that your window can be *redrawn* whenever it needs to be. Here's how you implement an OnPaint method:
def __init__(self):
...
EVT_PAINT(self, self.OnPaint)
...
def OnPaint(self, event):
dc = wxPaintDC(self)
<<your drawing code here>>
The thing is, your program will need to remember what it drew in the window originally, so that your OnPaint method can redraw it at the appropriate time. For example, you might use a list to hold all the points at which the user clicked. You can usually optimise things down by having a single method to draw into the window which can be called by either the OnPaint method, or by your user-initiated routine, kinda like this:
def __init__(self):
...
EVT_PAINT(self, self.OnPaint)
EVT_LEFT_DOWN(self, self.OnMouseClick)
self.clickPoints =
...
def OnPaint(self, event):
dc = wxPaintDC(self)
dc.BeginDrawing()
for point in self.clickPoints:
self.drawHit(dc, point)
dc.EndDrawing()
def OnMouseClick(self, event):
point = event.GetPosition()
dc = wxClientDC(self)
dc.BeginDrawing()
self.drawHit(dc, point)
dc.EndDrawing()
self.clickPoints.append(point)
def drawHit(self, dc, point):
dc.SetBrush(wxTRANSPARENT_BRUSH)
dc.SetPen(wxPen(wxNamedColour('RED3'), 2))
hitX, hitY = point.x, point.y
hitX = hitX + 58
hitY = hitY + 12
dc.DrawEllipse(hitX, hitY, 13, 13)
Hope this helps. WARNING: I haven't tested the above code -- but hopefully you can get the idea...
- Erik.