0

I'm working on a python app, and have hit a snag. My goal is to have a main frame containing numerous controls, and a panel which will be loaded from an xrc file specified at run time. Essentially, when a user clicks a button, I want a panel to be populated from an xrc file, the xrc file is chosen based on which button is clicked.

I'm defining the UI with XRCed, and here's what I have so far. This seems to load the sub-panel controls into the frame, but they aren't clickable, and they don't appear within the DynamicPanel as I hoped they would. Your help would be greatly appreciated.

EDIT

I've revised my main code, and now the controls appear where they in the frame, however the buttons still aren't clickable. Can anyone tell me why?

Python code:

import wx
import wx.xrc as xrc


from UNTITLED_xrc import xrcFRAME1

class XrcFrameSubClass(xrcFRAME1):
    """"""
    #----------------------------------------------------------------------
    def __init__(self):
        xrcFRAME1.__init__(self, parent = None)

        self.Bind(wx.EVT_BUTTON, self.OnButton_1, self.button1)
        self.Bind(wx.EVT_BUTTON, self.OnButton_2, self.button2)
        self.Bind(wx.EVT_BUTTON, self.OnButton_Clear, self.Button_Clear)

        self.Show()


    def OnButton_Clear(self, event):
        self.ClearSubPanel()

    def ClearSubPanel(self):
        try:
            self.SubPanel.Destroy()
            self.Fit()
            self.Layout()
        except:
            pass


    def OnButton_1(self, event):
        self.ClearSubPanel()

        SubPanel_xrc = '''<?xml version="1.0" ?><resource>
  <object class="wxPanel" name="SubPanel">
    <object class="wxBoxSizer">
      <orient>wxVERTICAL</orient>
      <object class="sizeritem">
        <object class="wxStaticText">
          <label>Your Mother was a</label>
        </object>
      </object>
      <object class="sizeritem">
        <object class="wxButton" name="SubButton1">
          <label>Hamster</label>
        </object>
      </object>
      <object class="sizeritem">
        <object class="wxButton" name="SubButton2">
          <label>Black Knight</label>
        </object>
      </object>
    </object>
  </object>
</resource>'''


        #PanelCoord = self.DynamicPanel.GetPosition()

        TopSizer = self.TopPanel.GetSizer()

        res = xrc.EmptyXmlResource() #Here it is
        res.LoadFromString(SubPanel_xrc)

        self.SubPanel = res.LoadPanel(self, "SubPanel")
        #Note: the first argument of the sizer.Insert() method is the index at which to insert the new item
        TopSizer.Insert(1,self.SubPanel,1, wx.EXPAND|wx.ALL, 5)

        self.Fit()
        self.Layout()

#        #Not sure how to do the event binding dynamically yet
#        self.SubButton1 = xrc.XRCCTRL(self.SubPanel, "SubButton1")
#        self.SubButton2 = xrc.XRCCTRL(self.SubPanel, "SubButton2")


    def OnButton_2(self, event):
        self.ClearSubPanel()

        SubPanel_xrc = '''<?xml version="1.0" ?>
<resource>
  <object class="wxPanel" name="SubPanel">
    <object class="wxBoxSizer">
      <orient>wxHORIZONTAL</orient>
      <object class="sizeritem">
        <object class="wxStaticText">
          <label>Your father smelt of</label>
        </object>
      </object>
      <object class="sizeritem">
        <object class="wxButton" name="SubButton1">
          <label>Elderberries</label>
        </object>
      </object>
      <object class="sizeritem">
        <object class="wxButton" name="SubButton2">
          <label>Shrubberies</label>
        </object>
      </object>
    </object>
  </object>
</resource>'''


        TopSizer = self.TopPanel.GetSizer()

        res = xrc.EmptyXmlResource() #Here it is
        res.LoadFromString(SubPanel_xrc)

        self.SubPanel = res.LoadPanel(self, "SubPanel")

        #Note: the first argument of the sizer.Insert() method is the index at which to insert the new item
        TopSizer.Insert(1,self.SubPanel,1, wx.EXPAND|wx.ALL, 5)

        self.Layout()
        self.Fit()

#        self.SubButton1 = xrc.XRCCTRL(self.SubPanel, "SubButton1")
#        self.SubButton2 = xrc.XRCCTRL(self.SubPanel, "SubButton2")

if __name__ == "__main__":
    app = wx.App(False)
    frame = XrcFrameSubClass()
    app.MainLoop()

MainFrame:

<?xml version="1.0" encoding="cp1252"?>
<resource>
  <object class="wxFrame" name="FRAME1">
    <title>This is the title</title>
    <object class="wxPanel" name="TopPanel">
      <object class="wxBoxSizer">
        <orient>wxVERTICAL</orient>
        <object class="sizeritem">
          <object class="wxBoxSizer">
            <orient>wxVERTICAL</orient>
            <object class="sizeritem">
              <object class="wxButton" name="button1">
                <label>Load SubXRC 1</label>
              </object>
              <option>1</option>
              <flag>wxGROW</flag>
            </object>
            <object class="sizeritem">
              <object class="wxButton" name="button2">
                <label>Load SubXRC 2</label>
              </object>
              <option>1</option>
              <flag>wxGROW</flag>
            </object>
            <object class="sizeritem">
              <object class="wxButton" name="Button_Clear">
                <label>Clear</label>
              </object>
            </object>
            <object class="spacer">
              <size>40,20</size>
            </object>
          </object>
        </object>
        <object class="sizeritem">
          <object class="wxPanel" name="DynamicPanel"/>
          <option>1</option>
          <flag>wxEXPAND</flag>
          <minsize>100,200</minsize>
        </object>
        <object class="sizeritem">
          <object class="wxBoxSizer">
            <orient>wxHORIZONTAL</orient>
            <object class="sizeritem">
              <object class="wxButton" name="button3">
                <label>button3</label>
              </object>
            </object>
            <object class="sizeritem">
              <object class="wxButton" name="button4">
                <label>button4</label>
              </object>
            </object>
          </object>
          <flag>wxALIGN_CENTRE</flag>
        </object>
      </object>
    </object>
  </object>
</resource>
nickvans
  • 898
  • 13
  • 24
  • Have you tried using wxFormBuilder to generate your XRC? It'll let you generate a single XRC file for the whole app, including your boxsizers, subpanels, etc. I find it much easier than mucking around with laying things out manually or using multiple XRC files. – John Lyon Nov 14 '12 at 05:08
  • I haven't, but I'll look into it. I'm still left with the problem of how to switch between which xrc block ought to be displayed in the panel, though. – nickvans Nov 14 '12 at 16:37
  • Try this question: http://stackoverflow.com/questions/2562063/why-does-hideing-and-showing-panels-in-wxpython-result-in-the-sizer-changi – John Lyon Nov 14 '12 at 23:18
  • 1
    I wasn't able to come up with a solution to my specific problem of wx controls not being active (clickable-- whatever.) So I ended up going with an alternate solution involving pre-defining a bunch of panels and hide/showing them. It's not as elegant a solution, but it seems to work. – nickvans Nov 16 '12 at 17:27

0 Answers0