-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_m_to_km.py
49 lines (40 loc) · 1.47 KB
/
convert_m_to_km.py
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
"""
CP1404 Week 11 Workshop - GUI program to convert miles to kilometres
Lindsay Ward, IT@JCU
06/10/2015
"""
from kivy.app import App
from kivy.lang import Builder
__author__ = 'Lindsay Ward'
MILES_TO_KM = 1.60934
class MilesConverterApp(App):
""" MilesConverterApp is a Kivy App for converting miles to kilometres """
def build(self):
""" build the Kivy app from the kv file """
self.title = "Convert Miles to Kilometres"
self.root = Builder.load_file('convert_m_km.kv')
return self.root
def handle_calculate(self):
""" handle calculation (could be button press or other call), output result to label widget """
value = self.get_validated_miles()
result = value * MILES_TO_KM
self.root.ids.output_label.text = str(result)
def handle_increment(self, change):
"""
handle up/down button press, update the text input with new value, call calculation function
:param change: the amount to change
"""
value = self.get_validated_miles() + change
self.root.ids.input_miles.text = str(value)
self.handle_calculate()
def get_validated_miles(self):
"""
get text input from text entry widget, convert to float
:return: 0 if error, float version of text if valid
"""
try:
value = float(self.root.ids.input_miles.text)
return value
except ValueError:
return 0
MilesConverterApp().run()