Skip to main content

wxcompose

CI

Declarative UI style and view model binding for wxPython

Installation

pip install wxcompose

Usage

See demo

Using components

Here is comparison between wxpython and wxcompose

import wx

app = wx.App()
frame = wx.Frame(None, title="Test", style=wx.DEFAULT_FRAME_STYLE | wx.CLIP_CHILDREN)
sizer = wx.BoxSizer(wx.VERTICAL)
txt = wx.StaticText(frame)
txt.SetLabel("Some text")
sizer.Add(txt, flag=wx.EXPAND | wx.ALL)

frame.Show()
app.MainLoop()
import wx

from wxcompose import core as wxc
from wxcompose.component import sizer_add

with wxc.App() as app:
    with wxc.Frame(title="Test", style=wx.DEFAULT_FRAME_STYLE | wx.CLIP_CHILDREN) as frame:
        with wxc.BoxSizer(orient=wx.VERTICAL):
            with wxc.StaticText() as _:
                _.Label = "Some text"
                sizer_add(flag=wx.EXPAND | wx.ALL)
        frame.Show()
    app.MainLoop()

wxcompose.core contains components for core wxpython controls, such as wx.Frame, wx.BoxSizer, wx.StaticText, etc. Examples of using generic component for controls

import wx
from wx.lib.agw import pygauge

from wxcompose import core as wxc
from wxcompose.component import Component, cmp, parent

with wxc.Frame(title="Test", style=wx.DEFAULT_FRAME_STYLE | wx.CLIP_CHILDREN) as frame:
    with Component(pygauge.PyGauge(frame)) as gauge:
        pass

    with Component(pygauge.PyGauge(parent())):
        pass

    with cmp(pygauge.PyGauge(parent())):
        pass

    with cmp(pygauge.PyGauge):
        pass

Binding

bind() can be used to bind control property to view model

from wxcompose import core as wxc
from wxcompose.binding import bind, to
from wxcompose.viewmodel import ViewModel


class TestViewModel(ViewModel):
    def __init__(self):
        super().__init__()
        self.name = "label"
        self.label = "label"


view_model = TestViewModel()

with wxc.StaticText() as _:
    bind(_).Label = to(lambda: f"{view_model.name}: {view_model.label}")

if control property should be updated only when some specific view model field is changed, 'when' expression can be passed as second argument

from wxcompose import core as wxc
from wxcompose.binding import bind, to
from wxcompose.viewmodel import ViewModel


class TestViewModel(ViewModel):
    def __init__(self):
        super().__init__()
        self.name = "label"
        self.label = "label"


view_model = TestViewModel()

with wxc.StaticText() as _:
    bind(_).Label = to(lambda: f"{view_model.name}: {view_model.label}", lambda: (view_model.name, view_model.label))

method call can be bind to view model changes. True can be passed as third argument to call passed method initially

from wxcompose import core as wxc
from wxcompose.binding import bind, to
from wxcompose.viewmodel import ViewModel


class TestViewModel(ViewModel):
    def __init__(self):
        super().__init__()
        self.ui_updated = self.custom_event("ui_updated")


view_model = TestViewModel()

with wxc.BoxSizer() as _:
    bind(_).call(lambda _: _.Layout(), lambda: view_model.ui_updated, True)

two way binding

import wx

from wxcompose import core as wxc
from wxcompose.binding import sync, to
from wxcompose.viewmodel import ViewModel


class TestViewModel(ViewModel):
    def __init__(self):
        super().__init__()
        self.value = 0


view_model = TestViewModel()

with wxc.TextCtrl() as _:
    sync(_, wx.EVT_TEXT, lambda v: int(v)).Value = to(view_model).value.map_(lambda v: str(v))

License

MIT

Release files for wxcompose 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for wxcompose 1.1.0
File Size Uploaded
wxcompose-1.1.0.tar.gz 13.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for wxcompose 1.1.0
File Interpreter ABI Platform
wxcompose-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size:25.4 kB

Release files / wxcompose-1.1.0.tar.gz

Download URL wxcompose-1.1.0.tar.gz
Size 13.0 kB
Tags Source
SHA-256 checksum
How to use checksums
4844f398d37baccea841459adf1903b5de5536e43050404a6db8dd4abbc590a5
BLAKE2b-256 checksum
How to use checksums
1b8010b13eb0abbe0ac4e1b5ce3396e0a21cc7e3718cc94d0b47d32bda2c1bc7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.23

Release files / wxcompose-1.1.0-py3-none-any.whl

Download URL wxcompose-1.1.0-py3-none-any.whl
Size 12.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c82cd7efb9be592dedaf3a5e9205555799bc34d983a8964066a0d91eddf7bf3a
BLAKE2b-256 checksum
How to use checksums
78863cfdea47a5c1995a651ad19d61b078fae4df5ffa9236f8cd0205a30b8cd5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.23

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page