Question
Miles to Kilometres Converter Files: convert_miles_km.py and convert_miles_km.kv Create a Python program (.py) and Kivy kv language file (.kv) for an app that converts miles
Miles to Kilometres Converter
Files: convert_miles_km.py and convert_miles_km.kv
Create a Python program (.py) and Kivy kv language file (.kv) for an app that converts miles to kilometres.
First, recreate the following layout shown in the image below. The dark grey with white text are Buttons, the black with yellow text is a Label and the black on white one is a TextInput.
When you have created the layout, write the functionality for the whole program. As always, do this in small steps.
Use a StringProperty for the text on the output (km) label. Then in the app you can set this property variable without having to manually update the text of the label. It will automatically update. See the mvc_demo.py and associated kv file in the demos for an example of this.
You should be able to type a number in the text entry field.
Pressing the "Convert" button should calculate the conversion from miles to kilometres.
Pressing the Up or Down buttons should make the miles number go up or down by 1.
Note: You can handle both of these with the same function by passing a value, e.g., handle_increment(-1) Or, you may like to pass the text of the input field when you call this, like: handle_increment(whatever_your_input_id_is.text, -1)
Stage 2 - Handle invalid inputs
If the text entered is not a valid number, just set the output result to 0.0. It should not crash or produce errors in the console.
Pressing Up or Down when the box is empty or invalid should assume the value is 0 and change it to 1 or -1.
Now, comment out the convert button in your kv file and make the result appear immediately when either text is entered or the up or down buttons are pressed. You can use Kivy's on_text event to respond to changes in the TextInput.
Did you use a CONSTANT in your conversion calculation? Do this now if you haven't already.
Note: The solution to this is provided (convert_miles_km.py/kv). Don't just copy it, but you're welcome to use it if you get stuck, or to compare your solution to ours.
.kv
BoxLayout: orientation: 'vertical' BoxLayout: orientation: 'horizontal' TextInput: id: input_miles text: '34' font_size: 48 on_text: app.handle_calculate() BoxLayout: orientation: 'vertical' size_hint_x: 0.25 Button: text: 'Up' on_press: app.handle_increment(1) Button: text: 'Down' on_press: app.handle_increment(-1) # Button: # text: 'Convert m to km' # on_press: app.handle_calculate() Label:
.py
""" 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_solution.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()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started