XRCed生成的代码分析

XRCed是wxPython附带的UI设计器,生成xrc资源文件,也可以输出python代码。本文对XRCed输出的python代码进行分析。

创建一个xrc文件如下,有两个窗口,每个窗口内一个按钮,UNTITLED.xrc:

<?xmlversion="1.0"encoding="utf-8"?>
<resource>
<objectclass="wxFrame"name="FRAME1">
<title></title>
<objectclass="wxPanel">
<objectclass="wxButton"name="myTestButton">
<label>BUTTON</label>
</object>
</object>
</object>
<objectclass="wxFrame"name="FRAME2">
<title></title>
<objectclass="wxPanel">
<objectclass="wxButton">
<label>BUTTON</label>
</object>
</object>
</object>
</resource>

然后生成python代码,UNTITLED_xrc.py:

# This file was automatically generated by pywxrc, do not edit by hand.
# -*- coding: UTF-8 -*-

importwx
importwx.xrc asxrc

__res = None

defget_resources():
"""This function provides access to the XML resources in this module."""
global__res
if__res == None:
__init_resources()
return__res


classxrcFRAME1(wx.Frame):
defPreCreate(self, pre):
"""This function is called during the class's initialization.

Override it for custom setup before the window is created usually to
set additional window styles using SetWindowStyle() and SetExtraStyle()."""
pass

def__init__(self, parent):
# Two stage creation (see http://wiki.wxpython.org/index.cgi/TwoStageCreation)
pre = wx.PreFrame()
self.PreCreate(pre)
get_resources().LoadOnFrame(pre, parent, "FRAME1")
self.PostCreate(pre)

# create attributes for the named items in this container
self.myTestButton = xrc.XRCCTRL(self, "myTestButton")


classxrcFRAME2(wx.Frame):
defPreCreate(self, pre):
"""This function is called during the class's initialization.

Override it for custom setup before the window is created usually to
set additional window styles using SetWindowStyle() and SetExtraStyle()."""
pass

def__init__(self, parent):
# Two stage creation (see http://wiki.wxpython.org/index.cgi/TwoStageCreation)
pre = wx.PreFrame()
self.PreCreate(pre)
get_resources().LoadOnFrame(pre, parent, "FRAME2")
self.PostCreate(pre)

# create attributes for the named items in this container



# ------------------------ Resource data ----------------------

def__init_resources():
global__res
__res = xrc.EmptyXmlResource()

__res.Load('UNTITLED.xrc')

从生成的Python代码可以看到:

* 只生成了Frame类,而不是一个可运行的Python程序。

为了运行显示上述的两个窗口,必须手工写如下代码:

importwx

importUNTITLED_xrc

app = wx.PySimpleApp()
frm1 = UNTITLED_xrc.xrcFRAME1(parent=None)
frm2 = UNTITLED_xrc.xrcFRAME2(parent=None)
frm1.Show()
frm2.Show()
app.MainLoop()

* 两个窗口资源在同一个文件中。

如果要分开多个文件,只能分多个xrc文件创建。

* 该自动生成文件不应该手工编辑,见头部:“do not edit by hand”。

所以对窗口类的自定义行为,如消息绑定,都需要继承该xrcFRAME。

其中有个“PreCreate()”,可以在子类中覆盖,对窗口进行预创建。

* 资源仅在使用到时才装载。所以分多个xrc资源是有利的。

* 对于命名控件,如“myTestButton”会自动创建,变量名相同。

(转载请注明来源于金庆的专栏)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值