Search code examples
python-3.xwxpython

wx.xml.XmlResource.Get().LoadPanel: doesn't work


Loading XRC wx.Panel on existing instance does not work.
There is no error reported, although on any attempt to display it's empty.

Erroneous method:

import wx
import wx.xrc

class SomePanelViewModel(wx.Panel):
    def __init__(self, parent):
        super().__init__()
        xmlResource = wx.xrc.XmlResource('SomePanelView.xrc')
        xmlResource.LoadPanel(panel=self, parent=parent, name='SomePanelView')

The XRC file is valid, because other method of LoadPanel works without a problem.
So the panel is populated and visible.

Working method:

xmlResource = wx.xrc.XmlResource('SomePanelView.xrc')
panel = xmlResource.LoadPanel(parent=parent, name='SomePanelView')

It is also worth to mention that both methods work for wx.Dialog without an issue.

I have also tried to run methods like 'Show' on broken panel. Unfortunately without any effect.

I'm startled and confused. Has anybody dealt with it before? Any suggestions? Solutions?


Solution

  • So in the end I'm answering my own question. xD

    The problem looks like a library bug.

    Therefore I decided to make a workaround:

    class XrcPanelView(wx.Panel):
    
        def __init__(self, parent):
            super().__init__(parent=parent)
    
            xrcFilePath = 'path/to/your.xrc'
            xmlResource = wx.xrc.XmlResource(xrcFilePath)
            self._xrcPanel = xmlResource.LoadPanel(parent=self, name='ViewName')
    
            sizer = wx.BoxSizer(wx.VERTICAL)
            sizer.Add(self._xrcPanel, 1, wx.EXPAND | wx.ALL, 0)
    

    That way you inherit a panel and apply you XRC panel on it.
    It's not a perfect solution.
    Although it achieves my goal.
    I needed to encapsulate some extra initialization steps within one view class.

    Hope somebody would enjoy. ;)