There are two ways to do this, both of which have samples in the demo. The first is to use a standard class (class="wxButton") and specify the optional subclass attribute that names the real class you want to use, (subclass="myModule.myButton")
The other way involves creating a class derived from XmlResourceHandler that knows how to handle creating instances of your custom class. This is exactly how all the other classes that XRC already knows how to construct are implemented, they all have a XmlResourceHandler.
···
On 8/24/12 2:36 PM, Waffle wrote:
Is there a way that I can put my own class in XRC, so I can be able to
do something like the following in the .xrc file?
<object class="myButton">
</object>
This would be my class:
class myButton(wx.Button):
def __init__(self,parent):
wx.Button.__init__(self,parent,-1,"My Special Button")
XRC error 17: no handler found for XML node “object” (class “SpecialTree”)
···
On Friday, 24 August 2012 22:38:38 UTC-3, Robin Dunn wrote:
On 8/24/12 2:36 PM, Waffle wrote:
Is there a way that I can put my own class in XRC, so I can be able to
do something like the following in the .xrc file?
This would be my class:
class myButton(wx.Button):
def __init__(self,parent):
wx.Button.__init__(self,parent,-1,"My Special Button")
Does anyone know how can I do this?
There are two ways to do this, both of which have samples in the demo.
The first is to use a standard class (class=“wxButton”) and specify the
optional subclass attribute that names the real class you want to use,
(subclass=“myModule.myButton”)
The other way involves creating a class derived from XmlResourceHandler
that knows how to handle creating instances of your custom class. This
is exactly how all the other classes that XRC already knows how to
construct are implemented, they all have a XmlResourceHandler.
The new handler needs to be created and added to the system before you try to use it. You're adding it after it is needed. Here is how it is done in the demo:
# Load the resource
res = xrc.EmptyXmlResource()
res.InsertHandler(MyCustomPanelXmlHandler())
res.LoadFromString(resourceText)
XRC error 17: no handler found for XML node “object” (class “SpecialTree”)
The new handler needs to be created and added to the system before you
try to use it. You’re adding it after it is needed. Here is how it is
done in the demo:
# Load the resource
res = xrc.EmptyXmlResource()
res.InsertHandler(MyCustomPanelXmlHandler())
res.LoadFromString(resourceText)
You are not specifying the sizer or the sizeritem properly in the XRC. (Sizer XML elements should contain sizeritem, not window elements directly.) For example, try this:
resourceText = r'''<?xml version="1.0"?>
<resource>
<object class="wxPanel" name="main">
<object class="wxBoxSizer">
<orient>wxVERTICAL</orient>
<object class="sizeritem">
<object class="MyCustomPanel" name="MyPanel">
<object class="wxStaticText" name="label1">
<label>This panel is a custom class derived from wx.Panel,\nand is loaded by a custom XmlResourceHandler.</label>
<pos>10,10</pos>
</object>
</object>
<option>1</option>
<flag>wxEXPAND</flag>
</object>
</object>
</object>
</resource>
'''
···
On 8/30/12 5:25 AM, Waffle wrote:
Here is a runnable sample, that produces the error: