Got it working - might be a good tutorial example?

wxPython Friends,

I got the simple reactangle drawing program working, and thought it might be good to share with all. Notice that it draws a rectangle upon initialization and then draws another one when you hit a key. The "key" to getting this to work was to recognize that dc class instances seem to REQUIRE an event to make them happen. The paint event was especially confusing to me at first.

Thom

And now with the attachment - I hate it when I do that...

Thom Ives wrote:

rectangle.py (2.53 KB)

···

wxPython Friends,

I got the simple reactangle drawing program working, and thought it might be good to share with all. Notice that it draws a rectangle upon initialization and then draws another one when you hit a key. The "key" to getting this to work was to recognize that dc class instances seem to REQUIRE an event to make them happen. The paint event was especially confusing to me at first.

Thom

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org

Thom Ives wrote:

And now with the attachment - I hate it when I do that...

Thom Ives wrote:

wxPython Friends,

I got the simple reactangle drawing program working, and thought it might be good to share with all. Notice that it draws a rectangle upon initialization and then draws another one when you hit a key. The "key" to getting this to work was to recognize that dc class instances seem to REQUIRE an event to make them happen. The paint event was especially confusing to me at first.

No, you can draw at any time with a wxClientDC. You just have to remember that when the __init__ is called that the window is not shown yet and so you won't see anything. The basic windows do not retain their drawn contents automatically, but send you an event when they need to be refreshed, (such as when the window is shown, or a portion of the window becomes unobscured by other windows.) There are things you can do to optimize the redraws so they don't take too much time or so there is minimal flicker, but you need to understand the basics first and you are on the right track for that.

One thing you are missing in your sample is a call to PrepareDC which is very important for wxScrolledWindow otherwise the position of the scrollbars is not taken into account and the drawing will always happen relative to the physical (0,0) rather than the logical (0,0). Here is your MyCanvas tweaked a little. Try commenting out the call to PrepareDC and then scrolling the window, covering it with another window and then scrolling it back to the top. Then put the PrepareDC back in and do the same thing.

class MyCanvas(wxScrolledWindow):
     def __init__(self, parent, id = -1, size = wxDefaultSize):
         wxScrolledWindow.__init__(self, parent, id,
                                   (0, 0), size, wxSUNKEN_BORDER)

         self.maxWidth = 1000
         self.maxHeight = 1000

         self.SetBackgroundColour("WHITE")
         self.SetScrollbars(20, 20,
                            self.maxWidth/20, self.maxHeight/20)

         self.DoDrawing(wxClientDC(self))
         EVT_PAINT(self, self.OnPaint)

     def DoDrawing(self, dc):
         self.PrepareDC(dc) # This is important for ScroledWindows
         dc.Clear()
         dc.BeginDrawing()
         for x, y, c in [ ( 5, 5, 'RED'),
                          ( 65, 65, 'BLUE'),
                          (125, 125, 'MAGENTA'), ]:
             dc.SetPen(wxPen(c, 3))
             dc.DrawRectangle(x, y, 50, 50)
         dc.EndDrawing()

     def OnPaint(self, evt):
         dc = wxPaintDC(self)
         self.DoDrawing(dc)

···

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