LoadObject() for dialog (xrc)

Robin Dunn wrote:

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")

Yes, I read it but "argument 2 of wxXmlResource_LoadObject()" was somehow misleading, I thought it is "customDlg", not "self".

Replacing "self" with "None" eliminated the error, thank you, but got me into another. A message box states:

"Unknown style flag wxDEFAULT_DIALOG_STYLE"
(though
         <style>wxDEFAULT_DIALOG_STYLE</style>
was generated by wxGlade)

and
"No handler found for XML node "object", class "CustomDlg"!"
(though there is such a handler, see below).

Thank you,

Cristina.

···

---------------
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.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

-------------------------------------------------------
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:

Robin Dunn wrote:

Yes, I read it but "argument 2 of wxXmlResource_LoadObject()" was somehow misleading, I thought it is "customDlg", not "self".

All methods have an implied first parameter, which is the object you are are calling the method on.

Replacing "self" with "None" eliminated the error, thank you, but got me into another. A message box states:

"Unknown style flag wxDEFAULT_DIALOG_STYLE"
(though
        <style>wxDEFAULT_DIALOG_STYLE</style>
was generated by wxGlade)

Your handler needs to do an AddStyle for this style. You call AddWindowStyles() but that only adds those styles that all wxWindows have in common. The C++ handler for wxDialog does these:

     XRC_ADD_STYLE(wxSTAY_ON_TOP);
     XRC_ADD_STYLE(wxCAPTION);
     XRC_ADD_STYLE(wxDEFAULT_DIALOG_STYLE);
     XRC_ADD_STYLE(wxTHICK_FRAME);
     XRC_ADD_STYLE(wxSYSTEM_MENU);
     XRC_ADD_STYLE(wxRESIZE_BORDER);
     XRC_ADD_STYLE(wxRESIZE_BOX);
     XRC_ADD_STYLE(wxDIALOG_MODAL);
     XRC_ADD_STYLE(wxDIALOG_MODELESS);

     XRC_ADD_STYLE(wxNO_3D);
     XRC_ADD_STYLE(wxTAB_TRAVERSAL);
     XRC_ADD_STYLE(wxWS_EX_VALIDATE_RECURSIVELY);
     XRC_ADD_STYLE(wxCLIP_CHILDREN);
     XRC_ADD_STYLE(wxMAXIMIZE_BOX);
     XRC_ADD_STYLE(wxMINIMIZE_BOX);

and
"No handler found for XML node "object", class "CustomDlg"!"
(though there is such a handler, see below).

Did you do an AddHandler with an instance of your handler class?

···

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