How to create wx widget subclass using XRC

In a 15-years old wxpython project I have the following source to create a subclass of wx.Dialog using XRC:

class NewDialog(wx.Dialog):
    def __init__(self, parent):
        res = xrc.XmlResource(XRC_NEWDIALOG_FILE)
        pre = wx.PreDialog()
        res.LoadOnDialog(pre, parent, XRC_NEWDIALOG)

To migrate this into new wxpython phoenix synthax, I only found a working, but for me not so nice solution:

class NewDialog(object):
    def __init__(self, parent):
        super(NewTreeDialog, self).__init__()
        res = xrc.XmlResource(XRC_NEWDIALOG_FILE)
        self.dlg = res.LoadDialog(parent, XRC_NEWDIALOG)

My problem is the NewDialog is not a subclass of wx.Dialog, but ‘has-a’ wx.Dialog. Change of this means to make my calling code like myNewDialog.Show() work, I need to add Show() method in NewDialog class or change the call to myMewDialog.dlg.Show()
Because I have a lot of subclass widgets using XRC and a huge amount of code, this change means, my migration need to add a lot of changes.

Do you have a better idea to solve my problem? Thanks in advance.

Try using the other overload of LoadDialog, like this:

    res.LoadDialog(self, parent, XRC_NEWDIALOG)

That should load the XRC content onto self.

1 Like

It works, thanks very much.
Have not understood the subtle difference of the overloaded method call, I checked the documentation wx.xrc.XmlResource — wxPython Phoenix 4.2.0 documentation
It’s actually written there. Just the example code there without the first parameter confused me.

Yes, I also noticed that the code snippet in the LoadDialog docs was incorrect. I’ve got a fix in the pipeline for that.