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:
Vincent Batts 2024-04-14 17:34:12 -04:00
parent c4767b5b56
commit 8eb15fe40b
Signed by: vbatts
GPG Key ID: E30EFAA812C6E5ED
2 changed files with 41 additions and 20 deletions

View File

@ -2,4 +2,3 @@ from helloworld.app import main
if __name__ == "__main__":
main().main_loop()

View File

@ -2,37 +2,59 @@
My first application
"""
import toga
from toga.style import Pack
from toga.style.pack import COLUMN, ROW
# from toga.style import Pack
from toga.style.pack import COLUMN, ROW, Pack, LEFT, RIGHT
class HelloWorld(toga.App):
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(
"Your name: ",
style=Pack(padding=(0, 5))
)
self.name_input = toga.TextInput(style=Pack(flex=1))
self.c_input = toga.TextInput(readonly=True)
self.f_input = toga.TextInput()
name_box = toga.Box(style=Pack(direction=ROW, padding=5))
name_box.add(name_label)
name_box.add(self.name_input)
c_label = toga.Label("Celcius", style=Pack(text_align=LEFT))
f_label = toga.Label("Farenheit", style=Pack(text_align=LEFT))
join_label = toga.Label("is equivalent to",
style=Pack(text_align=RIGHT))
hello_button = toga.Button(
"Say Hello!",
on_press=self.say_hello,
style=Pack(padding=5)
)
button = toga.Button("Calculate", on_press=self.calculate)
main_box.add(name_box)
main_box.add(hello_button)
f_box.add(self.f_input)
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.content = main_box
self.main_window.content = box
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):
if self.on_exit is not None:
self.on_exit()