From 8eb15fe40b6b59cf67f732e990278083037016d9 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Sun, 14 Apr 2024 17:34:12 -0400 Subject: [PATCH] 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 --- src/helloworld/__main__.py | 1 - src/helloworld/app.py | 60 ++++++++++++++++++++++++++------------ 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/helloworld/__main__.py b/src/helloworld/__main__.py index 36df9c9..db4c184 100644 --- a/src/helloworld/__main__.py +++ b/src/helloworld/__main__.py @@ -2,4 +2,3 @@ from helloworld.app import main if __name__ == "__main__": main().main_loop() - diff --git a/src/helloworld/app.py b/src/helloworld/app.py index 345f219..7bb8c1b 100644 --- a/src/helloworld/app.py +++ b/src/helloworld/app.py @@ -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()