LoadObject() for dialog (xrc)

When trying to adapt the wxXmlResourceHandler demo, in order to work with a dialog (not a panel), I get errors:

   File "customdlg1.py", line 57, in OnInit
     dlg = res.LoadObject(self, "customDlg", "CustomDlg")
   File "C:\Python22\lib\site-packages\wxPython\xrc.py", line 113, in LoadObject
     val = apply(xrcc.wxXmlResource_LoadObject,(self,) + _args, _kwargs)
TypeError: Type error in argument 2 of wxXmlResource_LoadObject. Expected _wxWindow_p.

#<customdlg1.py>

from wxPython.wx import *
from wxPython.xrc import *

RESFILE = "customdlg1.xrc"
resourceText = open(RESFILE).read()

class CustomDlg(wxDialog):
     def __init__(self, parent):
         wxDialog.__init__(self, parent, -1, "Cucu")
         self.SetBackgroundColour("RED")

class CustomDlgXmlHandler(wxXmlResourceHandler):
     def __init__(self):
         wxXmlResourceHandler.__init__(self)
         # Specify the styles recognized by objects of this type
         self.AddStyle("wxNO_3D", wxNO_3D)
         self.AddStyle("wxTAB_TRAVERSAL", wxTAB_TRAVERSAL)
         self.AddStyle("wxWS_EX_VALIDATE_RECURSIVELY", wxWS_EX_VALIDATE_RECURSIVELY)
         self.AddStyle("wxCLIP_CHILDREN", wxCLIP_CHILDREN)
         self.AddWindowStyles()

     # This method and the next one are required for XmlResourceHandlers
     def CanHandle(self, node):
         return self.IsOfClass(node, "CustomDlg")

     def DoCreateResource(self):
         # There is no existing instance. Be sure of that with an assert.
         assert self.GetInstance() is None

         # Now create the object
         dlg = CustomDlg(self.GetParentAsWindow(),
             self.GetID(),
             self.GetPosition(),
             self.GetSize(),
             self.GetStyle("style", wxTAB_TRAVERSAL),
             self.GetName()
             )
         # Set standard window attributes
         self.SetupWindow(dlg)
         # Create any child windows of this node
         self.CreateChildren(dlg)

         return dlg

···

#----------------------------------------------------------------------
def main():
     class TestApp(wxApp):
         def OnInit(self):
             # Load the resource
             res = wxEmptyXmlResource()
             res.InsertHandler(CustomDlgXmlHandler())
             res.LoadFromString(resourceText)

             # Now create a panel from the resource data
             dlg = res.LoadObject(self, "customDlg", "CustomDlg")

             # and do the layout
             sizer = dlg.GetSizer()
             sizer.Fit(dlg)
             sizer.SetSizeHints(dlg)
             dlg.Show(true)
             self.SetTopWindow(dlg)

             # Return a success flag
             return true

     app = TestApp(0)
     app.MainLoop()

if __name__ == '__main__':
  main()
-------------------------------
<customdlg1.xrc>

<?xml version="1.0"?>
<!-- generated by wxGlade 0.2 on Sun Jan 12 22:19:40 2003 -->

<resource version="2.3.0.1">
     <object class="CustomDlg" name="customDlg">
         <style>wxDEFAULT_DIALOG_STYLE</style>
         <size>331, 221</size>
         <title>Cucu Dlg</title>
         <object class="wxBoxSizer">
             <orient>wxHORIZONTAL</orient>
             <object class="sizeritem">
                 <option>1</option>
                 <flag>wxLEFT|wxRIGHT|wxALIGN_CENTER_VERTICAL</flag>
                 <border>75</border>
                 <object class="wxButton" name="cucuBtn">
                     <label>Cucu</label>
                 </object>
             </object>
         </object>
     </object>
</resource>

Any help is appreciated.

Cristina.

-------------------------------------------------------
Xnet scaneaza automat toate mesajele impotriva virusilor folosind RAV AntiVirus.
Xnet automatically scans all messages for viruses using RAV AntiVirus.

Nota: RAV AntiVirus poate sa nu detecteze toti virusii noi sau toate variantele lor.
Va rugam sa luati in considerare ca exista un risc de fiecare data cand deschideti
fisiere atasate si ca MobiFon nu este responsabila pentru nici un prejudiciu cauzat
de virusi.

Disclaimer: RAV AntiVirus may not be able to detect all new viruses and variants.
Please be aware that there is a risk involved whenever opening e-mail attachments
to your computer and that MobiFon is not responsible for any damages caused by
viruses.

C. Iacob wrote:

When trying to adapt the wxXmlResourceHandler demo, in order to work with a dialog (not a panel), I get errors:

  File "customdlg1.py", line 57, in OnInit
    dlg = res.LoadObject(self, "customDlg", "CustomDlg")
  File "C:\Python22\lib\site-packages\wxPython\xrc.py", line 113, in LoadObject
    val = apply(xrcc.wxXmlResource_LoadObject,(self,) + _args, _kwargs)
TypeError: Type error in argument 2 of wxXmlResource_LoadObject. Expected _wxWindow_p.

Did you read the error message? It's telling you that it wants a wxWindow but if you look at your code you're passing a wxApp. BTW, None should be a valid parent for top level windows like a wxDialog.

···

    class TestApp(wxApp):
        def OnInit(self):
            # Load the resource
            res = wxEmptyXmlResource()
            res.InsertHandler(CustomDlgXmlHandler())
            res.LoadFromString(resourceText)

            # Now create a panel from the resource data
            dlg = res.LoadObject(self, "customDlg", "CustomDlg")

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