使用 xml 资源文件的意图是将程序代码与界面分离。几个 GUI 构建器都运用这个概念来创建用户界面,比如有名的Glade。
专题:wxpython中文教程
例子中,创建了一个简单的有一个按钮的框架窗口。
从一个文件装入资源,载入一个面板以及将一个事件绑定到按钮上。
代码:
 
#!/usr/bin/python
#coding=utf-8
#xml.py
import wx
import wx.xrc as xrc
class Xml(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)
        
        res = xrc.XmlResource('resource.xrc')
        res.LoadPanel(self, 'MyPanel')
        
        self.Bind(wx.EVT_BUTTON, self.OnClose, id=xrc.XRCID('CloseButton'))
        self.Center()
        self.Show(True)
        
    def OnClose(self, event):
        self.Close()    
app = wx.App()
Xml(None, -1, 'xml.py')
app.MainLoop()
关 闭    150,100      
使用了两个调用来处理这些部件:
XRCID(resource_name) – 提供按钮或是菜单的id
XRCCTRL(resource_name) – 提供资源文件里的这些部件对应的事件处理器