In this tutorial let’s write Python Script to Distance Converter from KM to Yard, Mile and Foot using Tkinter
Python Code for Distance Converter from KM to Yard, Mile and Foot using Tkinter
app.py : You can save the below code with app.py
filename.
from tkinter import *
class UnitConverter(object):
def __init__(self, window):
self.window = window
l0 = Label(window, text = "Please input Km")
l0.grid(row = 0, column = 0)
l1 = Label(window, text = "Result:")
l1.grid(row = 1, column = 0)
l2 = Label(window, text = "Mile")
l2.grid(row = 2, column = 1)
l3 = Label(window, text = "Yard")
l3.grid(row = 3, column = 1)
l4 = Label(window, text = "Foot")
l4.grid(row = 4, column = 1)
self.entry_value = StringVar()
e0 = Entry(window, textvariable = self.entry_value, width = 15)
e0.grid(row = 0, column = 1)
b0 = Button(window, text = "Convert")
b0.grid(row = 1, column = 1)
self.t0 = Text(window, height = 1, width = 15)
self.t0.grid(row = 2, column = 0)
self.t1 = Text(window, height = 1, width = 15)
self.t1.grid(row = 3, column = 0)
self.t2 = Text(window, height = 1, width = 15)
self.t2.grid(row = 4, column = 0)
def km_to_mile(self):
mile = float(self.entry_value.get()) * 0.621371
mile = round(mile, 2)
self.t0.delete(1.0, END)
self.t0.insert(END, mile)
def km_to_yard(self):
yard = float(self.entry_value.get()) * 1093.61
yard = round(yard, 2)
self.t1.delete(1.0, END)
self.t1.insert(END, yard)
def km_to_foot(self):
foot = float(self.entry_value.get()) * 3280.84
foot = round(foot, 2)
self.t2.delete(1.0, END)
self.t2.insert(END, foot)
def convert(self):
self.km_to_mile()
self.km_to_yard()
self.km_to_foot()
window = Tk()
a = UnitConverter(window)
window.mainloop()
Output:
To execute the code through command line, you can use python3 app.py
Similar Posts: