Created
March 29, 2015 08:13
-
-
Save uranusjr/02ccb82ff02e5531ef6a to your computer and use it in GitHub Desktop.
Declarative styling inspired by django-crispy-forms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import toga | |
| f_input = toga.TextInput() | |
| c_input = toga.TextInput(readonly=True) | |
| c_label = toga.Label('Celcius', alignment=toga.LEFT_ALIGNED) | |
| f_label = toga.Label('Fahrenheit', alignment=toga.LEFT_ALIGNED) | |
| join_label = toga.Label('is equivalent to', alignment=toga.RIGHT_ALIGNED) | |
| def calculate(widget): | |
| try: | |
| c_input.value = (float(f_input.value) - 32.0) * 5.0 / 9.0 | |
| except Exception: | |
| c_input.value = '???' | |
| # RowContainer is a Container subclass that automatically sets | |
| # flexDirection='row'; ColumnContainer likewise. | |
| container = toga.ColumnContainer( | |
| # Positional arguments are child containers/widgets of a container. Keyword | |
| # arguments provide extra information, including a style object. | |
| toga.RowContainer( | |
| # Supply a tuple of (widget, style) is auto possible. Parameters of the | |
| # Style object will be applied automatically to the widget. This avoids | |
| # the need of setting f_input.style somewhere else, which can be easy to | |
| # overlook. | |
| ( | |
| f_input, | |
| toga.Style( | |
| flex=1, | |
| marginLeft=10, | |
| ), | |
| ), | |
| f_label, # Add a widget directly. | |
| style=toga.Style( | |
| margin=5, | |
| ), | |
| ), | |
| toga.RowContainer( # c_container | |
| join_label, | |
| ( | |
| c_input, | |
| { | |
| 'flex': 1, | |
| 'marginLeft': 10, | |
| }, | |
| ), | |
| c_label, | |
| # Maybe a dict is enough for style parameters? | |
| style={'margin': 5}, | |
| ), | |
| toga.Button( | |
| 'Calculate', | |
| on_press=calculate, | |
| # Widget classes should take a style parameter, too. | |
| style=toga.Style(margin=5), | |
| ), | |
| style=toga.Style(paddingTop=10), | |
| ) | |
| # Setting styles later should work, too. | |
| join_label.style(width=150, marginRight=10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment