Overview:
This version brings some great improvements for creating Widget-s, along with some utilities.
- Improve method
auto(): Better conversions & implicit calling byContainer - Rewrite method
WindowManager.run(): More modular, has better performance & stability - New method
WindowManager.bind(key, action): Assign a callback for a keypress - New feature setting arbitrary attributes in
Widgetconstruction
Details:
auto()
- Implicit calling whenever a non-widget is added to a
Container(or subclass, as long as its_add_widget()method is not overwritten) - Better conversions:
"text"->Label("text")
["label", lambda target, button: ...]->Button(label="label", onclick=lambda target, button: ...)
("Any", ["widget"], ...)->Splitter(Label("Any"), Button("widget", onclick=None), ...) - Note: There are some additional conversion models, but they are likely to change in the next release.
WindowManager.bind(key, action)
key-str: The trigger keyaction-Callable[[WindowManager], Any]: The method to be called- Note: If the
WindowManagercould find & execute a binding it assumes the key to be handled, and does not do any built-in handling.
Arbitrary attribute setting for Widget
Widgetis now passed**attrson construction- All of these attributes are applied using
setattr(), regardless of their respective fields - These changes should be integrated into custom
Widgetsubclasses:
class MyWidget(Widget):
def __init__(self) -> None:
super().__init__()⬇️
class MyWidget(Widget):
def __init__(self, **attrs: Any) -> None:
super().__init__(**attrs)End result
All of this combined results in the syntax for creating a simple window going from:
root = Window()
root.forced_width = 50
root.id = "root"
root += Label("[157 italic]This is a title")
root += Label()
root += Button("Exit", lambda *_: sys.exit()]To:
root = (
Window(forced_width=50, id="root")
+ "[157 italic]This is a title"
+ ""
+ ["Exit", lambda *_: sys.exit()]
)Do note, however, that the traditional syntax is still available. For more information on this version, check out the linked commit.
ok, bye 🚀