XRCed生成的代码分析

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

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

<?xml version="1.0" encoding="utf-8"?>
<resource>
  <object class="wxFrame" name="FRAME1">
    <title></title>
    <object class="wxPanel">
      <object class="wxButton" name="myTestButton">
        <label>BUTTON</label>
      </object>
    </object>
  </object>
  <object class="wxFrame" name="FRAME2">
    <title></title>
    <object class="wxPanel">
      <object class="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 -*-

import wx
import wx.xrc as xrc

__res = None

def get_resources():
    """ This function provides access to the XML resources in this module."""
    global __res
    if __res == None:
        __init_resources()
    return __res


class xrcFRAME1(wx.Frame):
    def PreCreate(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")


class xrcFRAME2(wx.Frame):
    def PreCreate(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程序。

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

import wx

import UNTITLED_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”会自动创建,变量名相同。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值