[wxPython-users] how to draw in a window, device context

Robin Dunn robin at alldunn.com
Mon Jun 4 09:55:31 PDT 2007


Chris Mellon wrote:
> On 6/4/07, 7stud <bbxx789_05ss at yahoo.com> wrote:
>> Hi,
>>
>> I am trying to draw a circle.  After reading about how to draw in a 
>> window
>>  with device contexts in "wxPython In Action ", the following is what 
>> I came
>> up with.  Of course it doesn't work.  Why?  Also, could someone explain
>> each line that needs to be added?
>>
>> Thanks
>>
> 
> <code snipped>
> 
> This "works" as far as it goes, but since you're drawing in the
> __init__ it gets erased when the window is actually show and
> repainted. You need to do your drawing in an EVT_PAINT handler
> instead. There's a chapter in wxPIA about this. I believe it's called
> "Implementing Custom Controls", but I don't have my copy handy so I

The book doesn't go into it as much as I would have liked to, but you 
are probably thinking about section 6.1.  There are a few other places 
as well that cover some aspects of drawing with DCs.   There are also 
lots of examples of custom drawing in the wiki.

The key point to remember is that no drawing is persistent.  As soon as 
the window is damaged in some way (being shown, resized, another window 
drags over the top of it, etc.) then it will send an EVT_PAINT event 
asking you to refresh the damaged portions.  If you don't respond to the 
EVT_PAINT then nothing will be redrawn (unless the widget has it's own 
handler for EVT_PAINT of course.)

-- 
Robin Dunn
Software Craftsman
http://wxPython.org  Java give you jitters?  Relax with wxPython!





More information about the wxpython-users mailing list