当前位置: 首页 > 工具软件 > XRCed > 使用案例 >

XRCed生成的代码分析

陈毅
2023-12-01

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

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

 类似资料: