-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfc_converter.py
More file actions
65 lines (49 loc) · 1.47 KB
/
fc_converter.py
File metadata and controls
65 lines (49 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import toga
from toga.constants import COLUMN, LEFT, RIGHT, ROW
def build(app):
# Fahrenheit box and widgets
f_box = toga.Box()
f_input = toga.TextInput()
f_label = toga.Label("Fahrenheit", text_align=LEFT)
# Celsius box and widgets
c_box = toga.Box()
c_input = toga.TextInput(readonly=True)
c_label = toga.Label("Celsius", text_align=LEFT)
join_label = toga.Label("is equivalent to", text_align=RIGHT)
# Calculate button
def calculate(widget):
try:
c_input.value = (float(f_input.value) - 32.0) * 5.0 / 9.0
except ValueError:
c_input.value = "???"
button = toga.Button("Calculate", on_press=calculate)
# Add widgets to boxes
f_box.add(f_input)
f_box.add(f_label)
c_box.add(join_label)
c_box.add(c_input)
c_box.add(c_label)
# Main box
box = toga.Box()
box.add(f_box)
box.add(c_box)
box.add(button)
# Styles
box.style.update(direction=COLUMN, margin=10, gap=10)
f_box.style.update(direction=ROW, gap=10)
c_box.style.update(direction=ROW, gap=10)
c_input.style.update(flex=1)
f_input.style.update(flex=1, margin_left=210)
c_label.style.update(width=100)
f_label.style.update(width=100)
join_label.style.update(width=200)
button.style.update(margin_top=5)
return box
def main():
return toga.App(
"Temperature Converter",
"org.beeware.toga.examples.tutorial",
startup=build,
)
if __name__ == "__main__":
main().main_loop()