[wxPython-users] OnPaint

Christopher Barker Chris.Barker at noaa.gov
Wed Oct 11 14:14:34 PDT 2006


Andrea Gavana wrote:
>> Which is why this structure is not recommended.
> 
> This is just a matter of personal taste.

Well, I like to think that it's a bit more than just taste. It is a 
matter of opinion, but I think clean, robust coding style can be 
assessed objectively.

 > AFAIK, both approaches work.

well, sure. so does this:

#!/usr/bin/env python
import wx

app = wx.App()
f = wx.Frame(None)
p = wx.Panel(f)
def Paint(event):
    dc = wx.PaintDC(p)
    dc.Clear()
    dc.DrawRectangle(20,20,50,50)
p.Bind(wx.EVT_PAINT, Paint)
app.SetTopWindow(f)
f.Show()
app.MainLoop()

But I doubt anyone would recommend that style.

> The question was just *why* that particular code didn't work.

Which you did answer perfectly accurately.

 > If then we move the discussion to coding styles,

Which was my intent.

The folks coming to this list are new to wxPython, and often also new to 
Python, and even new to programming and/or OO programming. I've kind of 
taken it upon my self to help encourage good coding style as early as 
possible. People will have better code and make fewer errors, the sooner 
they learn.

In fact, though the OP indicated that my proposed style would not have 
prevented the error, I'm not so sure -- he was (as was I, when I 
started) a bit confused about what the heck a wx.Window (vs. a wx.Panel, 
vs a wx.Frame) was -- that style makes the whole Frame-Panel-Window 
relationships more clear.

Just as a note, as it still may not be so clear -- you could accomplish 
the same thing without a Panel at all:

app = wx.App()
f = wx.Frame(None)
def Paint(event):
    dc = wx.PaintDC(f)
    dc.Clear()
    dc.DrawRectangle(20,20,50,50)
f.Bind(wx.EVT_PAINT, Paint)
app.SetTopWindow(f)
f.Show()
app.MainLoop()

The reason to add a Panel is if you want to put other controls on it, 
catch mouse events, etc, and/or want to put it on other Frames, Windows, 
etc. -- in that case, you'll really want a separate class.

-Chris






-- 
Christopher Barker, Ph.D.
Oceanographer
                                     		
NOAA/OR&R/HAZMAT         (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov




More information about the wxpython-users mailing list