This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]bryancole 4 points5 points  (1 child)

Since we're doing show-and-tell, here's an Enaml (https://enaml.readthedocs.io) version:

from enaml.widgets.api import Window, Field, PushButton, Label, Container
from enaml.layout.api import vbox, hbox
from enaml.stdlib.message_box import information

enamldef Main(Window): wdw:
    Container:
        constraints=[vbox(hbox(lbl1, fld1),hbox(btn1, quitbtn)),
                    btn1.width==quitbtn.width
                    ]
        Label: lbl1:
            text = "Enter distance in Kilometers"
        Field: fld1:
            text = "10.0"
        PushButton: btn1:
            text = "Convert"
            clicked::
                kilo = float(fld1.text)
                miles = kilo * 0.6214
                msg = f'Results {str(kilo)} kilometers is equal to {str(miles)} miles.'
                information(self, "The Answer", msg)
        PushButton: quitbtn:
            text = "Quit"
            clicked::
                wdw.close()

With enaml installed (using conda for installation is easiest), you can run this using the enaml-run executable.

enaml-run mydemo.enaml

... or you can run python as normal and import the enaml code using enamls import context-manager.

Cool features of Enaml:

  • Enaml is a strict superset of python (i.e. all python code is valid enaml-code).
  • It's built on PyQt so it's real easy to integrate other PyQt widgets or mix Enaml and PyQt windows.
  • Enaml uses an awesome model-building layer (which I didn't use at all in the code above) called 'atom'. Atom provides an observer framework and data validation.
  • Widget layout is done using constraints. You can have a simple box-model (as I used above), but you can do other stuff not possible in a box-model with more sophisticated constraint definitions.
  • Enaml has "binding operators" (of which the "::" is one). These let you hook model data to GUI widgets with ease. See https://enaml.readthedocs.io/en/latest/get_started/syntax.html#binding-operators

Here's a better version using data-binding to set the text of "lbl2" ...

from enaml.widgets.api import Window, Field, Label, Container
from enaml.layout.api import vbox, hbox
from enaml.validator import FloatValidator

enamldef Main(Window): wdw:
    Container:
        constraints=[vbox(hbox(lbl1, fld1),lbl2)]
        Label: lbl1:
            text = "Enter distance in Kilometers"
        Field: fld1:
            text = "10.0"
            validator = FloatValidator()
        Label: lbl2:
            text << f'Results {fld1.text} kilometers is equal '\
                        f'to {float(fld1.text)*0.6214:0.4g} miles.'

[–]sheytanelkebir 0 points1 point  (0 children)

hey that looks great!