Question

In: Computer Science

Creating a Kilometer to Miles Converter Graphical User Interaction is the way of our world today...

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

  1. Understand the basics of tkinter GUI development.
  2. Based on an informal application specification be able to develop a tkinter GUI program that contains one or more Label widgets.
  3. Be able to prompt user for input to a GUI application using a messagebox.
  4. Obtain user input into a GUI application that can be used for event driven selection.
  5. Be able to develop a tkinter GUI program that supports event-based widgets.
  6. Develop a tkinter GUI program that maintains application state using a Canvas widget and/or a Frame widget to pack other widgets.

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:

  • a text entry for the user to provide a kilometer input
  • two buttons labeled "Convert" and "Quit"

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.

Solutions

Expert Solution

Here our task is to create a GUI app which convert km to miles using tkinter

Points to remember before coding

  • Import tkinter module and message box
  • create a class called km_to_mi
  • in it method should contain all elements needed for our app such as button ,text box.(detials of their positons etc..)
  • we need a function called validate to check the input
  • Another function convert is needed to convert km to mile using formula(mile= km/1.609344)
  • a close function is used to destroy the application on pressing quit
  • in main function we have to call an instance of our class

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()


Related Solutions

As a youth of today, give five ways and means in creating a safe world.
As a youth of today, give five ways and means in creating a safe world.
Our economic world today is very different from the world of Adam Smith (1776) and Karl...
Our economic world today is very different from the world of Adam Smith (1776) and Karl Marx (1867) when they wrote their major economic works. Since then we have seen radical technological changes in production, the growth of transnational corporations, the end of the highly competitive economy of new businesses, the growth of the middle class in corporations and government (planners, analysts and technicians), and the origins of the liberal democratic institutions of government which, in most advanced capitalist countries,...
Income per capita disparity is a characteristic of our world today. What is NOT true about...
Income per capita disparity is a characteristic of our world today. What is NOT true about the state of the world in this sense? We have not seen convergence of GDP per capita We have not seen convergence of GDP per capita growth rates TFP has been stagnant in all countries. 28% of the population lives in a country with GDP per capita higher than the world GDP per capita.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT