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>
<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')
# -*- 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()
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”會自動創建,變量名相同。
(轉載請注明來源于金慶的專欄)