感想: xrc 只有單獨介面,所以若要取值或 binding 動作的話,就得自己再寫code處理,雖然已經方便許多,但是似乎還是有些煩瑣。
檔案:main.py, frame.py, gui.xrc
main.py
import wxframe.py
import wx.xrc as xrc
GUI_FILENAME = "gui.xrc"
GUI_MAINFRAME_NAME = "MainFrame"
class MyApp(wx.App):
def OnInit(self):
# Load all controls:
self._do_layout()
return True
def _do_layout(self):
self.res = xrc.XmlResource( GUI_FILENAME )
self.frame = self.res.LoadFrame( None, GUI_MAINFRAME_NAME )
self.SetTopWindow(self.frame)
self.frame.Show(1)
if __name__ == '__main__':
app = MyApp()
app.MainLoop()
import wxgui.xrc
import wx.xrc as xrc
class MainFrame(wx.Frame):
def __init__(self):
pre = wx.PreFrame()
# the Create step is done by XRC.
self.PostCreate(pre)
self.Bind(wx.EVT_WINDOW_CREATE, self.OnCreate)
def OnCreate(self, event):
self.Unbind(wx.EVT_WINDOW_CREATE)
wx.CallAfter(self._PostInit)
event.Skip()
return True
def _PostInit(self):
# Do all init here
self.Fit()
self.SetAutoLayout(True)
self.btnExit = xrc.XRCCTRL(self, 'btnExit')
self.Bind(wx.EVT_BUTTON, self.OnBtnExit, self.btnExit)
# Set properties:
self.btnExit.SetLabel('Exit')
def OnBtnExit(self, evt=None):
self.Close()
<?xml version="1.0" encoding="UTF-8"?>
<resource version="2.3.0.1">
<object class="wxFrame" name="demoframe" subclass="form.MainFrame">
<centered>1</centered>
<style>wxDEFAULT_FRAME_STYLE</style>
<title>Testing wx XRC mechanism</title>
<object class="wxBoxSizer">
<orient>wxHORIZONTAL</orient>
<object class="sizeritem">
<object class="wxPanel">
<object class="wxBoxSizer">
<orient>wxHORIZONTAL</orient>
<object class="sizeritem">
<object class="wxButton" name="btnExit">
<label></label>
<default>1</default>
</object>
<flag>wxALL|wxALIGN_CENTRE_VERTICAL|wxALIGN_CENTRE_HORIZONTAL</flag>
<border>20</border>
</object>
</object>
</object>
<flag>wxALL|wxEXPAND</flag>
</object>
</object>
</object>
</resource>
0 意見:
張貼留言