我正在开发一个python应用程序,并遇到了障碍。我的目标是拥有一个包含大量控件的主框架,以及一个将从运行时指定的xrc文件加载的面板。基本上,当用户单击按钮时,我希望从xrc文件中填充一个面板,根据单击的按钮选择xrc文件。
我用XRCed定义了UI,这是我到目前为止所拥有的。这似乎将子面板控件加载到框架中,但它们不可单击,并且它们不会像我希望的那样出现在DynamicPanel中。非常感谢您的帮助。
修改
我修改了我的主代码,现在控件出现在框架中的位置,但按钮仍然无法点击。谁能告诉我为什么?
Python代码:
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()
大型机:
<?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>