In: Computer Science
Using Python
Write a GUI program that converts a distance in Meters to the equivalent distance in Feet. The user should be able to enter a distance in Meters, click a button, and then see the equivalent distance in feet. Use the following formula to make the conversion:
Meters = Feet x 0.304
For example, 1 Meter is 3.28 Feet.
Solution: Python 3
import tkinter as tk
window = tk.Tk()
window.geometry("300x150")
window.title("Converter")
def convert():
meters = float(input_meter.get())
feets = meters / 0.304
output_feet.insert(0,'%.3f'%feets)
return
Meters = tk.Label(window, text="Enter Meters :", width=12, font=("arial",10,"bold"))
Meters.place(x=5, y=20)
input_meter = tk.Entry(window, width=15)
input_meter.place(x=105,y=20)
unit1 = tk.Label(window, text="Meters", width=6, font=("arial",10,"bold"))
unit1.place(x=150, y=20)
Feets = tk.Label(window, text="Result:", width=7, font=("arial",10,"bold"))
Feets.place(x=5, y=85)
output_feet = tk.Entry(window, width=15)
output_feet.place(x=105,y=85)
unit2 = tk.Label(window, text="Feets", width=6, font=("arial",10,"bold"))
unit2.place(x=150, y=85)
button = tk.Button(window, text="Convert", width=12, bg="brown", fg="white", command=convert)
button.place(x= 100, y=50)
window.mainloop()
OUTPUT