金庆的专栏

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  64 随笔 :: 0 文章 :: 169 评论 :: 0 Trackbacks

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():
    """ Thisfunction 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 theclass's initialization.
        
        Overrideit for custom setup before the window is created usually to
        setadditional 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 inthis container
        self.myTestButton= xrc.XRCCTRL(self, "myTestButton")


class xrcFRAME2(wx.Frame):
    def PreCreate(self, pre):
        """ This function is called during theclass's initialization.
        
        Overrideit for custom setup before the window is created usually to
        setadditional 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 inthis container



# ------------------------ Resourcedata ----------------------

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

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

posted on 2008-03-26 13:23 金庆 阅读(1200) 评论(5)  编辑 收藏 引用 所属分类: 2. Python

评论

# re: XRCed生成的代码分析 2008-03-27 10:53 haskell
xrc可否自定义控件的缩放行为?
假如有两个控件,垂直排列,下面的只进行左右伸缩,不进行上下伸缩,
上面的填满
我偶尔用用wxWidget,那些布局我始终没看明白
还有对应WTL中的DoDataExchange是什么函数?  回复  更多评论
  

# re: XRCed生成的代码分析 2008-03-31 09:25 金庆
@haskell
当然可以。如下,设上下放大比例为1:0,即下面的按钮不进行上下伸缩,并且设置wxEXPAND,即左右扩展。
wxBoxSizer* sizer_1 = new wxBoxSizer(wxVERTICAL);
sizer_1->Add(button_1, 1, wxEXPAND, 0);
sizer_1->Add(button_2, 0, wxEXPAND, 0);
SetSizer(sizer_1);
上面的代码是wxGlade生成的。  回复  更多评论
  

# re: XRCed生成的代码分析 2008-03-31 10:01 金庆
@haskell
DoDataExchange()等效的功能在wxWidgets中应该是“wxValidator”吧。
A validator is an object that can be plugged into a control (such as a wxTextCtrl), and mediates between C++ data and the control, transferring the data in either direction and validating it.
我还没用过,有机会可以试试。  回复  更多评论
  

# re: XRCed生成的代码分析 2008-04-02 12:47 haskell
不错,谢谢,我写的第一个wxPython程序是画分形,感觉List空间不是很大,想用c++实现算法部分,然后用boost_python绑定,交由python画图,可是关于指针绑定,stl容器绑定这些东西不大好学。不知道这种模式是否值得花时间去研究,就是用c++实现算法,python写界面  回复  更多评论
  

# re: XRCed生成的代码分析 2008-04-03 09:25 金庆
@haskell
就是要费点时间。学习过程是愉快的。  回复  更多评论