Using Custom ComponentsΒΆ

We have seen that we can refer to standard wx components such as buttons and static text fields from the XML. What about custom components that are not inside the wx namespace?

humblewx can be configured to look for components anywhere. We just need to modify the humblewx.COMPONENT_MODULES configuration variable.

Say we have this custom component that we want to use in a dialog:

class CustomComponent(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        label = wx.StaticText(self, label="this is a custom component")
        button = wx.Button(self, label="click me")
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(label)
        sizer.Add(button)
        self.SetSizer(sizer)

In the XML we can just refer to this component by name as we do with any other wx component:

class CustomComponentExampleDialog(humblewx.Dialog):

    """
    <BoxSizerVertical>
        <StaticText label="before custom component" />
        <CustomComponent />
        <StaticText label="after custom component" />
    </BoxSizerVertical>
    """

    def __init__(self, parent):
        humblewx.Dialog.__init__(self, humblewx.Controller, parent)

It looks like this:

_images/custom_component.png

In order for this to work, we have to tell humblewx that it should also look for components in another module. In this example, we only have one module where both the custom component and the dialog are defined. We can get a reference to that module with the following code:

sys.modules[__name__]

Next we need to modify humblewx.COMPONENT_MODULES to include this module:

humblewx.COMPONENT_MODULES.append(sys.modules[__name__])

We need to run this code before we create our dialog.

We can add any module to this list:

import foo.bar
humblewx.COMPONENT_MODULES.append(foo.bar)