[wxPython-users] A module to help load frame and dialog from XRC resource easily.

Aaron Brady castironpi at comcast.net
Tue Jan 15 11:06:07 PST 2008


>>>From: Gary Lee [mailto:garywlee at gmail.com] 
Sent: Sunday, January 13, 2008 8:53 PM

Another note on this: in order to make this module generally useful,
you'll need to add support for handling menus and toolbars as well. 

 Thanks for your suggestions. I am looking for a way to support menu and
toolbars. However, I never use Menu and Toolbar from XRC resource before.
Still have no idea. Any cool idea?<<<
<<
For those of us who like subclassing (I don't, but from here, I can't rule
it out as useless): Generate a new class, which you can subclass, for each
control loaded from an XRC.  Mix and match to taste.  Example use:
	class FrameA( XRCFrame ):# (line 15, and)
	frame= FrameA.XRCInit( res, None, 'MyFrame1' )# (line 26)

import  wx
import  wx.xrc  as  xrc

classns= [ 'Frame', 'Button', 'Gauge' ] #etc.
classtupes= [ ( 'XRC%s'% cl, 'wx.%s'% cl, '"wx%s"'% cl ) for cl in classns ]
for classn, wxclassn, xrcclassn in classtupes:
	classdef= '''class %s( %s ):
	@classmethod
	def XRCInit( cls, rcres, parent, name ):
		self= rcres.LoadObject( parent, name, %s )
		return self'''% ( classn, wxclassn, xrcclassn )
	print classdef
	exec classdef

class FrameA( XRCFrame ):
	def dummy( self ):
		print 'a method in Frame1\n',

guipath= '..\\guis\\wxFormBuilder\\'
filen= guipath+ 'myprojectdesign1.xrc' #use yours here
filetext= open( filen ).read()
res= xrc.XmlResource.Get()
res.LoadFromString( filetext )

app= wx.PySimpleApp()
frame= FrameA.XRCInit( res, None, 'MyFrame1'  )
sizer= wx.BoxSizer( wx.VERTICAL )
button1= wx.Button( frame )
sizer.Add( button1, 0, wx.EXPAND )
frame.SetSizer( sizer )
frame.Show()
app.MainLoop()




More information about the wxpython-users mailing list