app: moving on to a Toga tutorial (F to C conversion)
https://toga.readthedocs.io/en/stable/tutorial/tutorial-1.html did a little adaptation of their example to make it a class, rather than a build() Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
parent
c4767b5b56
commit
8eb15fe40b
2 changed files with 41 additions and 20 deletions
|
@ -2,4 +2,3 @@ from helloworld.app import main
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main().main_loop()
|
main().main_loop()
|
||||||
|
|
||||||
|
|
|
@ -2,37 +2,59 @@
|
||||||
My first application
|
My first application
|
||||||
"""
|
"""
|
||||||
import toga
|
import toga
|
||||||
from toga.style import Pack
|
# from toga.style import Pack
|
||||||
from toga.style.pack import COLUMN, ROW
|
from toga.style.pack import COLUMN, ROW, Pack, LEFT, RIGHT
|
||||||
|
|
||||||
|
|
||||||
class HelloWorld(toga.App):
|
class HelloWorld(toga.App):
|
||||||
def startup(self):
|
def startup(self):
|
||||||
main_box = toga.Box(style=Pack(direction=COLUMN))
|
c_box = toga.Box()
|
||||||
|
f_box = toga.Box()
|
||||||
|
box = toga.Box()
|
||||||
|
|
||||||
name_label = toga.Label(
|
self.c_input = toga.TextInput(readonly=True)
|
||||||
"Your name: ",
|
self.f_input = toga.TextInput()
|
||||||
style=Pack(padding=(0, 5))
|
|
||||||
)
|
|
||||||
self.name_input = toga.TextInput(style=Pack(flex=1))
|
|
||||||
|
|
||||||
name_box = toga.Box(style=Pack(direction=ROW, padding=5))
|
c_label = toga.Label("Celcius", style=Pack(text_align=LEFT))
|
||||||
name_box.add(name_label)
|
f_label = toga.Label("Farenheit", style=Pack(text_align=LEFT))
|
||||||
name_box.add(self.name_input)
|
join_label = toga.Label("is equivalent to",
|
||||||
|
style=Pack(text_align=RIGHT))
|
||||||
|
|
||||||
hello_button = toga.Button(
|
button = toga.Button("Calculate", on_press=self.calculate)
|
||||||
"Say Hello!",
|
|
||||||
on_press=self.say_hello,
|
|
||||||
style=Pack(padding=5)
|
|
||||||
)
|
|
||||||
|
|
||||||
main_box.add(name_box)
|
f_box.add(self.f_input)
|
||||||
main_box.add(hello_button)
|
f_box.add(f_label)
|
||||||
|
|
||||||
|
c_box.add(join_label)
|
||||||
|
c_box.add(self.c_input)
|
||||||
|
c_box.add(c_label)
|
||||||
|
|
||||||
|
box.add(f_box)
|
||||||
|
box.add(c_box)
|
||||||
|
box.add(button)
|
||||||
|
|
||||||
|
box.style.update(direction=COLUMN, padding=10)
|
||||||
|
f_box.style.update(direction=ROW, padding=5)
|
||||||
|
c_box.style.update(direction=ROW, padding=5)
|
||||||
|
|
||||||
|
self.c_input.style.update(flex=1)
|
||||||
|
self.f_input.style.update(flex=1, padding_left=210)
|
||||||
|
c_label.style.update(width=100, padding_left=10)
|
||||||
|
f_label.style.update(width=100, padding_left=10)
|
||||||
|
join_label.style.update(width=200, padding_left=10)
|
||||||
|
|
||||||
|
button.style.update(padding=15)
|
||||||
|
|
||||||
self.main_window = toga.MainWindow(title=self.formal_name)
|
self.main_window = toga.MainWindow(title=self.formal_name)
|
||||||
self.main_window.content = main_box
|
self.main_window.content = box
|
||||||
self.main_window.show()
|
self.main_window.show()
|
||||||
|
|
||||||
|
def calculate(self, widget):
|
||||||
|
try:
|
||||||
|
self.c_input.value = (float(self.f_input.value) - 32.0) * 5.0 / 9.0
|
||||||
|
except:
|
||||||
|
self.c_input.value = "???"
|
||||||
|
|
||||||
def quit(self, widget):
|
def quit(self, widget):
|
||||||
if self.on_exit is not None:
|
if self.on_exit is not None:
|
||||||
self.on_exit()
|
self.on_exit()
|
||||||
|
|
Loading…
Reference in a new issue