An alternative xrc subclassing approach

I've been using wxWindows/wxWidgets/wxPython for over 7 years now and
find wxPython fantastic for quickly developing prototypes and converting
those prototypes into applications. Thanks to all the developers that
make this possible. I have recently been playing with a technique for
sub classing objects created via XRC and am wondering whether this
technique is a good idea for production use (it works fine in testing).

I have always found the method for sub classing objects with XRC
somewhat cumbersome. I really do not want or need a new xrc subclass
for objects that are only referenced once, like a panel in a notebook.
Recently I realized that -- since wxPython is really just creating
proxies that reference wxWidget classes -- there is no reason why I
could not create a second wxPython class with my custom methods. This
allows me to handle all the events etc. in a custom class without
needing to modify my xrc definition. An example will illustrate this
better:

# Untested code:
class MyDialog(wx.Dialog):
    def __init__(self):
       wx.PostCreate(wx.PreDialog())
       xrc.XmlResource_Get( ).LoadOnDialog( self, None, "MyDialog" )

       # panel will ref. the wxPython class created automatically during
LoadOnDialog:
       panel = self.FindWindowByName("MyPanel")

       # myPanel will ref. the same wxWidgets object as panel but it
has my custom methods:
       myPanel=MyPanel(panel)
       myPanel.myCustomMethod()

class MyPanel(wx.Panel):
    def __init__(self,panel):
       self.PostCreate(panel) # Now this instance references the
wxWidget object too.

···

#
       # Setup event handlers etc to implement the functionality of the
panel etc.
       self.Bind(...)

    def myCustomMethod(self):
        # blah, blah...

This technique allows me to use the xrc editor to design a dialog with
multiple panels and then add the functionality to each panel as needed.
I could do this directly in MyDialog but it is nice to move the panel
specific behavior to it's own class. I could use the xrc extension
mechanism but that requires my XRC definition to be changed and I can no
longer simply preview the dialog and see all of details of each panel.
To me the technique outlined above is simpler, although I will use the
xrc extension mechanism for situations where I actually WANT the ability
to reference a custom control in my xrc definition (like if I make a
custom wx.MaskedEditControl).

This technique works fine in testing (I'm using the 2.5.3.2pre Debian
package) but I am curious if any one else sees any problems that I have
overlooked. If this issue has already been discussed on this list then
I apologize and request a reference to that thread (I did do a search
before posting and could not find anything). Any comments are appreciated,

Shawn Church
http://SChurchComputers.com

Shawn Church wrote:

       # panel will ref. the wxPython class created automatically during
LoadOnDialog:
       panel = self.FindWindowByName("MyPanel")

       # myPanel will ref. the same wxWidgets object as panel but it
has my custom methods:
       myPanel=MyPanel(panel)
       myPanel.myCustomMethod()

class MyPanel(wx.Panel):
    def __init__(self,panel):
       self.PostCreate(panel) # Now this instance references the
wxWidget object too.
       #
       # Setup event handlers etc to implement the functionality of the
panel etc.
       self.Bind(...)

This technique works fine in testing (I'm using the 2.5.3.2pre Debian
package) but I am curious if any one else sees any problems that I have
overlooked.

I don't think there will be any problems.

···

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

This is the technique I am using and I haven encountered any
problems. But my app is finished so it hasnt alot of testing...

Shawn Church wrote:

···

I've been using wxWindows/wxWidgets/wxPython for over 7 years now and
find wxPython fantastic for quickly developing prototypes and converting
those prototypes into applications. Thanks to all the developers that
make this possible. I have recently been playing with a technique for
sub classing objects created via XRC and am wondering whether this
technique is a good idea for production use (it works fine in testing).

I have always found the method for sub classing objects with XRC
somewhat cumbersome. I really do not want or need a new xrc subclass
for objects that are only referenced once, like a panel in a notebook. Recently I realized that -- since wxPython is really just creating
proxies that reference wxWidget classes -- there is no reason why I
could not create a second wxPython class with my custom methods. This
allows me to handle all the events etc. in a custom class without
needing to modify my xrc definition. An example will illustrate this
better:

# Untested code:
class MyDialog(wx.Dialog):
    def __init__(self):
       wx.PostCreate(wx.PreDialog())
       xrc.XmlResource_Get( ).LoadOnDialog( self, None, "MyDialog" )

       # panel will ref. the wxPython class created automatically during
LoadOnDialog:
       panel = self.FindWindowByName("MyPanel")

       # myPanel will ref. the same wxWidgets object as panel but it
has my custom methods:
       myPanel=MyPanel(panel)
       myPanel.myCustomMethod()

class MyPanel(wx.Panel):
    def __init__(self,panel):
       self.PostCreate(panel) # Now this instance references the
wxWidget object too.
       #
       # Setup event handlers etc to implement the functionality of the
panel etc.
       self.Bind(...)

    def myCustomMethod(self):
        # blah, blah...

This technique allows me to use the xrc editor to design a dialog with
multiple panels and then add the functionality to each panel as needed. I could do this directly in MyDialog but it is nice to move the panel
specific behavior to it's own class. I could use the xrc extension
mechanism but that requires my XRC definition to be changed and I can no
longer simply preview the dialog and see all of details of each panel. To me the technique outlined above is simpler, although I will use the
xrc extension mechanism for situations where I actually WANT the ability
to reference a custom control in my xrc definition (like if I make a
custom wx.MaskedEditControl).

This technique works fine in testing (I'm using the 2.5.3.2pre Debian
package) but I am curious if any one else sees any problems that I have
overlooked. If this issue has already been discussed on this list then
I apologize and request a reference to that thread (I did do a search
before posting and could not find anything). Any comments are appreciated,

Shawn Church
http://SChurchComputers.com

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

Hi,

Sorry for this being slightly offtopic, but I had no luck at the
py2exe and reportlab lists (which are definitely not as frequented as
this list!!!), but usually most people like to ship gui apps as
executables and that's why I give it a shot here:
A quite simple thing:
I'd like to convert pythonpoint.py from reportlab into an executable
so I can call it from my program. I have never has any trouble with
py2exe before and it compiles the python code just fine. When I run
it, it says that stdparser.pyc from reportlab says ImportError:'no module named
customshapes'.
Has anybody any clue what I'm missing? I am quite down since I am
supposed to ship that little tool wednesday ...
My setup.py:
from distutils.core import setup
import py2exe
        
setup(console=["pythonpoint.py"])

Thanks a lot.

···

--
Best regards,
the_shelter mailto:pdftex@the-shelter.de

Hi,

the_shelter wrote:

Hi,

Sorry for this being slightly offtopic, but I had no luck at the
py2exe and reportlab lists (which are definitely not as frequented as
this list!!!), but usually most people like to ship gui apps as
executables and that's why I give it a shot here:
A quite simple thing:
I'd like to convert pythonpoint.py from reportlab into an executable
so I can call it from my program. I have never has any trouble with
py2exe before and it compiles the python code just fine. When I run
it, it says that stdparser.pyc from reportlab says ImportError:'no module named
customshapes'.
Has anybody any clue what I'm missing? I am quite down since I am
supposed to ship that little tool wednesday ...
My setup.py:
from distutils.core import setup
import py2exe
       setup(console=["pythonpoint.py"])

I don't know reportlab but it looks/sounds to me as if py2exe doesn't find all dependencies. I had a similar issue with matplotlib and solve it like this in the setup.py.

mpldata = glob.glob(r'C:\Python24\share\matplotlib\*')
mpldata.append(r'C:\Python24\share\matplotlib\.matplotlibrc')

      data_files = [("prog\\locale\\fr\\LC_MESSAGES",
                       mylocaleFR),
                    ("prog\\locale\\de\\LC_MESSAGES",
                       mylocaleDE),
                    ("prog\\locale\\en\\LC_MESSAGES",
                       mylocaleEN),
Above is my local stuff, then there are some other dependencies and/or things like license.txt files etc and below you see the matplotlib and the python 2.4 stuff.
                    ("lib\\matplotlibdata", mpldata),
                    ("prog\\", python4dll)
                    ]

Hope this helps.

Werner

···

Thanks a lot.

Hello Werner,

Monday, June 13, 2005, 1:06:37 PM, you wrote:

Hi,

the_shelter wrote:

Hi,

Sorry for this being slightly offtopic, but I had no luck at the
py2exe and reportlab lists (which are definitely not as frequented as
this list!!!), but usually most people like to ship gui apps as
executables and that's why I give it a shot here:
A quite simple thing:
I'd like to convert pythonpoint.py from reportlab into an executable
so I can call it from my program. I have never has any trouble with
py2exe before and it compiles the python code just fine. When I run
it, it says that stdparser.pyc from reportlab says ImportError:'no module named
customshapes'.
Has anybody any clue what I'm missing? I am quite down since I am
supposed to ship that little tool wednesday ...
My setup.py:
from distutils.core import setup
import py2exe
       
setup(console=["pythonpoint.py"])

I don't know reportlab but it looks/sounds to me as if py2exe doesn't
find all dependencies. I had a similar issue with matplotlib and solve
it like this in the setup.py.

mpldata = glob.glob(r'C:\Python24\share\matplotlib\*')
mpldata.append(r'C:\Python24\share\matplotlib\.matplotlibrc')

      data_files = [("prog\\locale\\fr\\LC_MESSAGES",
                       mylocaleFR),
                    ("prog\\locale\\de\\LC_MESSAGES",
                       mylocaleDE),
                    ("prog\\locale\\en\\LC_MESSAGES",
                       mylocaleEN),
Above is my local stuff, then there are some other dependencies and/or
things like license.txt files etc and below you see the matplotlib and
the python 2.4 stuff.
                    ("lib\\matplotlibdata", mpldata),
                    ("prog\\", python4dll)
                    ]

Hope this helps.

Werner

Thanks a lot.

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

thanks- this took care of it.

Cheers,

···

--
Best regards,
the_shelter mailto:pdftex@the-shelter.de

Well, I tried pretty hard to "break" instances defined in this way and
apart from the obvious (don't call Destroy method) I could not find any
problems -- all the methods returned correct results. My main reason
for asking the question was that the specific behavior of PostCreate()
is potentially subject to change and a future release of wx.Python might
not support this method. But if Robin thinks it should be OK then it
probably will be.

Let me know if you run into anything.

Shawn

Toni Brkic wrote:

···

This is the technique I am using and I haven encountered any
problems. But my app is finished so it hasnt alot of testing...