Python XML resource compiler - pywxrc

Pywxrc is a module that can help you a great deal when working with XML Resources (XRC). It can generate classes for each of the top-level windows (Frames and Dialogs) with most of the boilerplate code you need to write when doing it yourself. It can also embed the resource file and any bitmaps it depends on in the resulting Python module. This results in code that is much easier to maintain, especially if you use XRCed which can use pywxrc to generate the Python code each time you save the XRC file.

Pywxrc is available in the trunk of the wxPython CVS, and you can get it from

(previously)

Note that the pywxrc that is currently bundled with wxPython 2.6.x.x is an old version that can only embed XRC files in Python module and doesn't have any code generation functionality.

Demo

This is a simple application that uses pywxrc (rev. 1.8 from CVS), based on the example for PyxBoil. You'll need to run python pywxrc.py pywxrc_demo.xrc to generate the boilerplate code in pywxrc_demo_xrc.py before you run pywxrc_demo.py.

pywxrc_demo.py

import wx

from pywxrc_demo_xrc import *

class MyMainFrame(xrcMyMainFrame):
    def __init__(self, parent):
        # Initialize the frame
        xrcMyMainFrame.__init__(self, parent)
        
        # Wire up the events (still has to be done manually)
        self.Bind(wx.EVT_BUTTON, self.OnOk, self.OkButton)

    def OnOk(self, e):
        # Get the content of the text controls & display
        message = "You are " + self.firstName.GetValue() + " " + self.lastName.GetValue() + "!"
        dlg = wx.MessageDialog(self, message, "Hmm...", wx.OK | wx.ICON_INFORMATION)
        dlg.ShowModal()
        dlg.Destroy()

class MyApp(wx.App):
    def OnInit(self):
        # Display the frame
        self.frame = MyMainFrame(None)
        self.frame.Center()
        self.frame.Show(1)
        return True

if __name__ == '__main__':
    app = MyApp()
    app.MainLoop()

pywxrc_demo.xrc

<?xml version="1.0" encoding="cp1255"?>
<resource>
  <object class="wxFrame" name="MyMainFrame">
    <title></title>
    <object class="wxPanel">
      <object class="wxBoxSizer">
        <orient>wxVERTICAL</orient>
        <object class="sizeritem">
          <object class="wxStaticText">
            <label>So, who are you?</label>
            <font>
              <size>12</size>
              <family>default</family>
              <style>normal</style>
              <weight>normal</weight>
              <underlined>0</underlined>
            </font>
          </object>
          <flag>wxALL</flag>
          <border>5</border>
        </object>
        <object class="spacer">
          <size>0,15</size>
        </object>
        <object class="sizeritem">
          <object class="wxBoxSizer">
            <orient>wxHORIZONTAL</orient>
            <object class="sizeritem">
              <object class="wxStaticText">
                <label>First Name:</label>
                <size>80,20</size>
              </object>
            </object>
            <object class="spacer">
              <size>5,0</size>
            </object>
            <object class="sizeritem">
              <object class="wxTextCtrl" name="firstName"/>
              <option>1</option>
              <flag>wxEXPAND</flag>
            </object>
          </object>
          <flag>wxALL|wxEXPAND</flag>
          <border>5</border>
        </object>
        <object class="sizeritem">
          <object class="wxBoxSizer">
            <orient>wxHORIZONTAL</orient>
            <object class="sizeritem">
              <object class="wxStaticText">
                <label>Last Name:</label>
                <size>80,20</size>
              </object>
            </object>
            <object class="spacer">
              <size>5,0</size>
            </object>
            <object class="sizeritem">
              <object class="wxTextCtrl" name="lastName"/>
              <option>1</option>
              <flag>wxEXPAND</flag>
            </object>
          </object>
          <flag>wxALL|wxEXPAND</flag>
          <border>5</border>
        </object>
        <object class="spacer">
          <option>1</option>
        </object>
        <object class="sizeritem">
          <object class="wxBoxSizer">
            <orient>wxHORIZONTAL</orient>
            <object class="spacer">
              <size>0,0</size>
              <option>1</option>
            </object>
            <object class="sizeritem">
              <object class="wxButton" name="OkButton">
                <label>OK</label>
                <style>wxSUNKEN_BORDER</style>
              </object>
            </object>
            <object class="spacer">
              <size>5,0</size>
            </object>
          </object>
          <flag>wxEXPAND</flag>
        </object>
        <object class="spacer">
          <size>0,10</size>
        </object>
      </object>
      <size>240,180</size>
    </object>
    <style></style>
  </object>
</resource>

pywxrc_demo_xrc.py

This is the file that is generated automatically by pywxrc for the pywxrc_demo.xrc XRC file.

# 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 xrcMyMainFrame(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, "MyMainFrame")
        self.PostCreate(pre)

        # Define variables for the controls
        self.firstName = xrc.XRCCTRL(self, "firstName")
        self.lastName = xrc.XRCCTRL(self, "lastName")
        self.OkButton = xrc.XRCCTRL(self, "OkButton")



# ------------------------ Resource data ----------------------

def __init_resources():
    global __res
    __res = xrc.EmptyXmlResource()

    __res.Load('pywxrc_demo.xrc')

pywxrc (last edited 2017-06-06 03:26:56 by ffx)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.