Skip to main content

Declarative UI style and view model binding for wxPython

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wxcompose-1.1.0.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wxcompose-1.1.0-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file wxcompose-1.1.0.tar.gz.

File metadata

  • Download URL: wxcompose-1.1.0.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.23

File hashes

Hashes for wxcompose-1.1.0.tar.gz
Algorithm Hash digest
SHA256 4844f398d37baccea841459adf1903b5de5536e43050404a6db8dd4abbc590a5
MD5 e1ed286da7f81f3326bae4478fe55bbe
BLAKE2b-256 1b8010b13eb0abbe0ac4e1b5ce3396e0a21cc7e3718cc94d0b47d32bda2c1bc7

See more details on using hashes here.

File details

Details for the file wxcompose-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: wxcompose-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.23

File hashes

Hashes for wxcompose-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c82cd7efb9be592dedaf3a5e9205555799bc34d983a8964066a0d91eddf7bf3a
MD5 333bb2a41db9671e7a98517eade6a235
BLAKE2b-256 78863cfdea47a5c1995a651ad19d61b078fae4df5ffa9236f8cd0205a30b8cd5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page