wx.xrc.XmlResource¶
This is the main class for interacting with the XML-based resource system.
The class holds XML
resources from one or more .xml files, binary files or zip archive files.
Note that this is a singleton class and you’ll never allocate/deallocate it. Just use the static wx.xrc.XmlResource.Get
getter.
See also
XML Based Resource System , XRC File Format
Class Hierarchy¶
Methods Summary¶
Constructor. |
|
Initializes only a specific handler (or custom handler). |
|
Registers subclasses factory for use in |
|
Attaches an unknown control to the given panel/window/dialog. |
|
Removes all handlers and deletes them (this means that any handlers added using |
|
Compares the |
|
Returns a string |
|
Gets the global resources object or creates one if none exists. |
|
Returns the domain (message catalog) that will be used to load translatable strings in the |
|
Returns flags, which may be a bitlist of wx.xrc.XmlResourceFlags enumeration values. |
|
Returns the wx.xml.XmlNode containing the definition of the object with the given name or |
|
Returns version information (a.b.c.d = d + 256c + 2562b + 2563a). |
|
Returns a numeric |
|
Initializes handlers for all supported controls/windows. |
|
Add a new handler at the beginning of the handler list. |
|
Loads resources from |
|
Loads all .xrc files from directory dirname. |
|
Loads a bitmap resource from a file. |
|
Loads a dialog. |
|
Load resources from the |
|
Simpler form of |
|
Loads a frame from the resource. |
|
Load the resource from a bytes string or other data buffer compatible object. |
|
Loads an icon resource from a file. |
|
Loads menu from resource. |
|
Loads a menubar from resource. |
|
Load an object from the resource specifying both the resource name and the class name. |
|
Load an object from anywhere in the resource tree. |
|
Loads a panel. |
|
Loads a toolbar. |
|
Sets the global resources object and returns a pointer to the previous one (may be |
|
Sets the domain (message catalog) that will be used to load translatable strings in the |
|
Sets flags (bitlist of wx.xrc.XmlResourceFlags enumeration values). |
|
This function unloads a resource previously loaded by |
Properties Summary¶
See |
Class API¶
- class wx.xrc.XmlResource(Object)¶
Possible constructors:
XmlResource(filemask, flags=XRC_USE_LOCALE, domain='') -> None XmlResource(flags=XRC_USE_LOCALE, domain='') -> None
This is the main class for interacting with the XML-based resource system.
Methods¶
- __init__(self, *args, **kw)¶
-
__init__ (self, filemask, flags=XRC_USE_LOCALE, domain=’’)
Constructor.
- Parameters:
filemask (string) – The
XRC
file, archive file, or wildcard specification that will be used to load all resource files inside a zip archive.flags (int) – One or more value of the wx.xrc.XmlResourceFlags enumeration.
domain (string) – The name of the gettext catalog to search for translatable strings. By default all loaded catalogs will be searched. This provides a way to allow the strings to only come from a specific catalog.
- Return type:
None
__init__ (self, flags=XRC_USE_LOCALE, domain=’’)
Constructor.
- Parameters:
flags (int) – One or more value of the wx.xrc.XmlResourceFlags enumeration.
domain (string) – The name of the gettext catalog to search for translatable strings. By default all loaded catalogs will be searched. This provides a way to allow the strings to only come from a specific catalog.
- Return type:
None
- AddHandler(self, handler)¶
Initializes only a specific handler (or custom handler).
Convention says that the handler name is equal to the control’s name plus ‘XmlHandler’, for example TextCtrlXmlHandler, HtmlWindowXmlHandler.
The
XML
resource compiler (wxxrc) can create include file that contains initialization code for all controls used within the resource. Note that this handler must be allocated on the heap, since it will be deleted byClearHandlers
later.- Parameters:
handler (wx.xrc.XmlResourceHandler)
- Return type:
None
- static AddSubclassFactory(factory)¶
Registers subclasses factory for use in
XRC
.This is useful only for language bindings developers who need a way to implement subclassing in wxWidgets ports that don’t support
RTTI
(e.g. wxPython).- Parameters:
factory (wx.xrc.XmlSubclassFactory)
- Return type:
None
- AttachUnknownControl(self, name, control, parent=None)¶
Attaches an unknown control to the given panel/window/dialog.
Unknown controls are used in conjunction with <object class=”unknown”>.
- ClearHandlers(self)¶
Removes all handlers and deletes them (this means that any handlers added using
AddHandler
must be allocated on the heap).- Return type:
None
- CompareVersion(self, major, minor, release, revision)¶
Compares the
XRC
version to the argument.Returns -1 if the
XRC
version is less than the argument, +1 if greater, and 0 if they are equal.- Parameters:
major (int)
minor (int)
release (int)
revision (int)
- Return type:
int
- static FindXRCIDById(numId)¶
Returns a string
ID
corresponding to the given numericID
.The string returned is such that calling
GetXRCID
with it as parameter yields numId. If there is no string identifier corresponding to the given numeric one, an empty string is returned.Notice that, unlike
GetXRCID
, this function is slow as it checks all of the identifiers used inXRC
.- Parameters:
numId (int)
- Return type:
str
Added in version 2.9.0.
- static Get()¶
Gets the global resources object or creates one if none exists.
- Return type:
- GetDomain(self)¶
Returns the domain (message catalog) that will be used to load translatable strings in the
XRC
.- Return type:
str
- GetFlags(self)¶
Returns flags, which may be a bitlist of wx.xrc.XmlResourceFlags enumeration values.
- Return type:
int
- GetResourceNode(self, name)¶
Returns the wx.xml.XmlNode containing the definition of the object with the given name or
None
.This function recursively searches all the loaded
XRC
files for an object with the specified name. If the object is found, the wx.xml.XmlNode corresponding to it is returned, so this function can be used to access additional information defined in theXRC
file and not used by wx.xrc.XmlResource itself, e.g. contents of application-specificXML
tags.- Parameters:
name (string) – The name of the resource which must be unique for this function to work correctly, if there is more than one resource with the given name the choice of the one returned by this function is undefined.
- Return type:
- Returns:
The node corresponding to the resource with the given name or
None
.
- GetVersion(self)¶
Returns version information (a.b.c.d = d + 256c + 2562b + 2563a).
- Return type:
int
- static GetXRCID(str_id, value_if_not_found=ID_NONE)¶
Returns a numeric
ID
that is equivalent to the stringID
used in anXML
resource.If an unknown str_id is requested (i.e. other than
ID_XXX
or integer), a new record is created which associates the given string with a number.If value_if_not_found is
ID_NONE
, the number is obtained viawx.NewId
. Otherwise value_if_not_found is used.Macro
XRCID(name)
is provided for convenient use in event tables.- Parameters:
str_id (string)
value_if_not_found (int)
- Return type:
int
Note
IDs returned by XRCID() cannot be used with the
EVT_*_RANGE
macros, because the order in which they are assigned to symbolic name values is not guaranteed.
- InitAllHandlers(self)¶
Initializes handlers for all supported controls/windows.
This will make the executable quite big because it forces linking against most of the wxWidgets library.
- Return type:
None
- InsertHandler(self, handler)¶
Add a new handler at the beginning of the handler list.
- Parameters:
handler (wx.xrc.XmlResourceHandler)
- Return type:
None
- Load(self, filemask)¶
Loads resources from
XML
files that match given filemask.Example:
if not wx.xml.XmlResource.Get().Load("rc/*.xrc"): wx.LogError("Couldn't load resources!")
- Parameters:
filemask (string)
- Return type:
bool
Note
If
USE_FILESYS
is enabled, this method understands wx.FileSystem URLs (seewx.FileSystem.FindFirst
).Note
If you are sure that the argument is name of single
XRC
file (rather than an URL or a wildcard), useLoadFile
instead.See also
- LoadAllFiles(self, dirname)¶
Loads all .xrc files from directory dirname.
Tries to load as many files as possible; if there’s an error while loading one file, it still attempts to load other files.
- Parameters:
dirname (string)
- Return type:
bool
Added in version 2.9.0.
- LoadBitmap(self, name)¶
Loads a bitmap resource from a file.
- Parameters:
name (string)
- Return type:
- LoadDialog(self, *args, **kw)¶
-
LoadDialog (self, parent, name)
Loads a dialog.
parent points to parent window (if any).
LoadDialog (self, dlg, parent, name)
Loads a dialog.
parent points to parent window (if any).
This form is used to finish creation of an already existing instance (the main reason for this is that you may want to use derived class with a new event table). Example:
# Without a class dlg = wx.Dialog() wx.xml.XmlResource.Get().LoadDialog(dlg, mainFrame, "my_dialog") dlg.ShowModal() # Or, as a class class MyDialog(wx.Dialog): def __init__(self, parent): super().__init__() wx.xml.XmlResource.Get().LoadDialog(self, parent, "my_dialog")
- LoadDocument(self, doc, name='')¶
Load resources from the
XML
document containing them.This can be useful when
XRC
contents comes from some place other than a file or, more generally, an URL, as it can still be read into a MemoryInputStream and then wx.xml.XmlDocument can be created from this stream and used with this function.For example:
xrc_data = ... # Retrieve it from wherever. xmlDoc = wx.xml.XmlDocument(io.BytesIO(xrc_data)) if not xmlDoc.IsOk(): ... handle invalid XML here ... return if not wx.XmlResource.Get().LoadDocument(xmlDoc): ... handle invalid XRC here ... return ... use the just loaded XRC as usual ...
- Parameters:
doc (wx.xml.XmlDocument) – A valid, i.e. non-null, document pointer ownership of which is passed to wx.xrc.XmlResource, i.e. this pointer can’t be used after this function returns.
name (string) – The name argument is optional, but may be provided if you plan to call
Unload
later. It doesn’t need to be an existing file or even conform to the usual form of file names as it is not interpreted in any way by wx.xrc.XmlResource, but it should be unique among the other documents and file names used if specified.
- Return type:
bool
- Returns:
True
on success,False
if the document couldn’t be loaded (note that doc is still destroyed in this case to avoid memory leaks).
Added in version 4.1/wxWidgets-3.1.6.
- LoadFile(self, file)¶
Simpler form of
Load
for loading a singleXRC
file.- Parameters:
file (string)
- Return type:
bool
Added in version 2.9.0.
See also
- LoadFrame(self, *args, **kw)¶
-
LoadFrame (self, parent, name)
Loads a frame from the resource.
parent points to parent window (if any).
LoadFrame (self, frame, parent, name)
Loads the contents of a frame onto an existing wx.Frame.
This form is used to finish creation of an already existing instance (the main reason for this is that you may want to use derived class with a new event table).
- LoadFromBuffer(self, data)¶
Load the resource from a bytes string or other data buffer compatible object.
- Return type:
bool
- LoadIcon(self, name)¶
Loads an icon resource from a file.
- Parameters:
name (string)
- Return type:
- LoadMenu(self, name)¶
Loads menu from resource.
Returns
None
on failure.- Parameters:
name (string)
- Return type:
- LoadMenuBar(self, *args, **kw)¶
-
LoadMenuBar (self, parent, name)
Loads a menubar from resource.
Returns
None
on failure.- Parameters:
parent (wx.Window)
name (string)
- Return type:
LoadMenuBar (self, name)
- Parameters:
name (string)
- Return type:
- LoadObject(self, *args, **kw)¶
-
LoadObject (self, parent, name, classname)
Load an object from the resource specifying both the resource name and the class name.
The first overload lets you load nonstandard container windows and returns
None
on failure. The second one lets you finish the creation of an existing instance and returnsFalse
on failure.In either case, only the resources defined at the top level of
XRC
files can be loaded by this function, useLoadObjectRecursively
if you need to load an object defined deeper in the hierarchy.LoadObject (self, instance, parent, name, classname)
- Parameters:
- Return type:
bool
- LoadObjectRecursively(self, *args, **kw)¶
-
LoadObjectRecursively (self, parent, name, classname)
Load an object from anywhere in the resource tree.
These methods are similar to
LoadObject
but may be used to load an object from anywhere in the resource tree and not only the top level. Note that you will very rarely need to do this as in normal use the entire container window (defined at the top level) is loaded and not its individual children but this method can be useful in some particular situations.Added in version 2.9.1.
LoadObjectRecursively (self, instance, parent, name, classname)
- Parameters:
- Return type:
bool
- LoadPanel(self, *args, **kw)¶
-
LoadPanel (self, parent, name)
Loads a panel.
parent points to the parent window.
LoadPanel (self, panel, parent, name)
Loads a panel.
parent points to the parent window. This form is used to finish creation of an already existing instance.
- LoadToolBar(self, parent, name)¶
Loads a toolbar.
- Parameters:
parent (wx.Window)
name (string)
- Return type:
- static Set(res)¶
Sets the global resources object and returns a pointer to the previous one (may be
None
).- Parameters:
res (wx.xrc.XmlResource)
- Return type:
- SetDomain(self, domain)¶
Sets the domain (message catalog) that will be used to load translatable strings in the
XRC
.- Parameters:
domain (string)
- Return type:
None
- SetFlags(self, flags)¶
Sets flags (bitlist of wx.xrc.XmlResourceFlags enumeration values).
- Parameters:
flags (int)
- Return type:
None
- Unload(self, filename)¶
This function unloads a resource previously loaded by
Load
.Returns
True
if the resource was successfully unloaded andFalse
if it hasn’t been found in the list of loaded resources.- Parameters:
filename (string)
- Return type:
bool
Properties¶
- Version¶
See
GetVersion