In: Computer Science
Creating a Kilometer to Miles Converter
Graphical User Interaction is the way of our world today as we know it. This week you will have an opportunity to design and develop perhaps your first GUI app.
Lab Assignment Objectives
Understand the Application
For this GUI app you will create a kilometer to miles distance converter. Specifically we will write a class that will implement converting distances in kilometers to miles.
Implement a GUI app that contains:
Invoking the app, the user will be able to supply a kilometer value into the kilometer entry widget prompt.
When the user clicks the first button, Convert, the kilometers to miles conversion result will be displayed in an information dialog box. When the user clicks the second button, Quit, the app closes.
The Program Specification
Write a program that converts distances in kilometers to miles. The task for this lab is to develop an Object Oriented GUI application as a class that encapsulates the implementation of converting distances in kilometers to miles. Display the result in an info dialog box.
Testing Specification
Create an instance of the Kilometer Converter GUI class to demonstrate your app.
Input Error Checking: Validate user Input into the entry widget (i.e. a valid kilometer value). Do check that the button clicked performs the correct action (i.e. the convert button invokes the callback function for the conversion, the quit button closes the app).
Test Run Requirements: Provide a screenshot of your GUI program console display.
Here our task is to create a GUI app which convert km to miles using tkinter
Points to remember before coding
Now let's see how to code it
CODE
(Please read all comments for better understanding of the program)
Sample Run
I am also attching the text version of the code in case you need to copy paste
import tkinter as tk #importing tkinter
from tkinter import messagebox #importing message box
class km_to_mi(tk.Frame): #creating a class
def __init__(self, parent): #__init___ method
window.title("Kilometers to Miles Converter") # this function
included all graphical elements needed for our app
window.geometry("400x250")
self.label1 = tk.Label(window, text="Enter Kilometer:")
self.label1.place(x=70,y=50)
self.textbox1 = tk.Entry(window, width=12)
#textbox
self.textbox1.place(x=200,y=50)
self.btn1 = tk.Button(window, text="Convert",
command=self.validate) #convert button
self.btn1.place(x=120,y=150)
self.btn2 = tk.Button(window,
text="Quit",command=self.close) #quit button
self.btn2.place(x=220,y=150)
def convert(self,val): #function for convert km to
mile
if val > 0:
kilometers = round(float(val)/1.609344,5) #conversion equation
implemented here
messagebox.showinfo("Information",str(kilometers)+" Miles")
else:
messagebox.showerror("Error", "Kilometer must be positive")
def validate(self):
if self.textbox1.get().isdigit(): #checking whether number or
not
self.convert(float(self.textbox1.get()))
else:
messagebox.showerror("Error", "Kilometer must be a number")
def close(self): #function for quit button
window.destroy()
if __name__ == "__main__": #main function starts here
window= tk.Tk()
km_to_mi(window) #instance of our class is created here
window.mainloop()