XMLUI

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

XMLUI

Chris Norman

Hi all,

I'm happy to announce the first release of XMLUI: My quicker-and-dirty implementation of xrc.


The idea is you create structure in an XML file, then overlay that structure on an already-created wx.Frame or wx.Dialog instance.


Here's the xml for a login frame that's provided with the repo:


<frame>
    <title>Test Frame</title>
    <sizer orient="vertical" name="main_sizer">
        <sizer>
            <label style="st_ellipsize_end">&amp;Username</label>
            <text name="username" sizer_proportion="1" sizer_flag="grow" style="te_rich2">test</text>
        </sizer>
        <sizer>
            <label style="st_ellipsize_end">&amp;Password</label>
            <text name="password" sizer_proportion="1" sizer_flag="grow" style="te_password" bind="text_paste:on_paste,text_copy:on_copy"></text>
        </sizer>
        <sizer>
            <label>&amp;Age</label>
            <integer name="age" sizer_proportion="1" sizer_flag="grow" min="10">18</integer>
        </sizer>
        <sizer>
            <label>&amp;Height (in metres)</label>
            <float name="height" sizer_proportion="1" sizer_flag="grow" digits="2" min="0.5" max="2.5" increment="0.1">1.5</float>
        </sizer>
        <sizer>
            <label>Rating</label>
            <slider style="sl_horizontal,sl_inverse" name="rating" min="0" max="5">3</slider>
        </sizer>
        <sizer>
            <label>Agree</label>
            <checkbox name="agree" sizer_proportion="1" sizer_flag="grow" label="I &amp;Agree"></checkbox>
        </sizer>
        <sizer sizer_proportion="1" sizer_flag="grow">
            <button name="login" label="&amp;Login" default="1" bind="button:on_login"></button>
            <button name="cancel" label="&amp;Cancel" bind="button:close"></button>
        </sizer>
    </sizer>
</frame>


Here's the Python code that drives it:


"""Show a frame generated from an XML file."""

import wx
from xmlui.wx import WXXMLParser


class MyXMLParser(WXXMLParser):
    """Add event handlers."""

    def on_paste(self, event):
        """Pop up an alert."""
        return wx.MessageBox('You shouldn\'t copy and paste passwords.')

    def on_copy(self, event):
        """Pop up an alert."""
        return wx.MessageBox(
            'You probably won\'t get any useful text that way.'
        )

    def on_login(self, event):
        frame = event.GetEventObject().GetParent().GetParent()
        username = frame.username.GetValue()
        password = frame.password.GetValue()
        return wx.MessageBox(
            'You clicked the Login button.\n\n'
            f'Username: {username}\n'
            f'Password: {password}'
        )

    def close(self, event):
        event.GetEventObject().GetParent().GetParent().Close(True)


if __name__ == '__main__':
    a = wx.App()
    f = wx.Frame(None)
    xml.populate_from_file('frame.xml', f, None)
    f.Show(True)
    f.Maximize()
    a.MainLoop()


As you can see, you can bind multiple events for each control. Also, anything in there (apart from the root tag - which can be anything), with a name attribute will be added to the frame with that name.


Todo:

Currently there is no work around for lines like:

frame.Bind(wx.EVT_MENU, handler, menu_item)

There is currently no documentation beyond docstrings. I'll add that at some point in the hopefully-not-too-distant future (hopefully tomorrow).


If I (or someone else) wants that functionality I could add it easily enough.


If anyone has any suggestions or whatever, please feel free to message me.


Github: Link

PyPi: Link

Complaints: [hidden email]

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.