金庆的专栏

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  423 随笔 :: 0 文章 :: 454 评论 :: 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 金庆 阅读(3530) 评论(9)  编辑 收藏 引用 所属分类: 6. 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
就是要费点时间。学习过程是愉快的。  回复  更多评论
  

# re: XRCed生成的代码分析 2010-06-15 04:56 JuanaBonner33
Our life is really strange issue and sometime people can do one only thing at the same time, then you don’t have time to work on some else stuff. Sometimes, some students should opt for 'tween party time and <a href="http://www.manyessays.com/essays/literature">literature essays paper</a> accomplishing. In this situation, I advice to search for the experienced cheap essay writing service to buy the <a href="http://www.manyessays.com">essay</a> close to this good topic from.   回复  更多评论
  

# re: XRCed生成的代码分析 2010-07-18 10:39 get ringtones
I would like to thank you a lot for a really good release and as well, want to give you the monophonic ringtones at the best get ringtones Internet sites.   回复  更多评论
  

# re: XRCed生成的代码分析 2010-11-09 02:25 dissertation writing
I do think that this is manageable to see this web page, just because only here scholars could find the hot enough thought close to this topic. So, the dissertation service should utilize that for student dissertation performing.   回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理