By Dr. Angela Yu
Day 27 of 100: Tkinter, *args, **kwargs, and Creating GUI Programs
Using TkInter, develop an application that converts miles to kilometers.
This application is written with Python 3.11.
The application displays two interactive pieces:
- A form input box where a user will enter the number of miles they want converted to kilometers
- A
Calculate
button that triggers the conversion and outputs the kilometers.
This project uses one library:
- TkInter
Tkinter is used to create a Graphic User Interface (GUI) with a width of 250 pixels and a height of 120 pixels:
window = Tk()
window.title("Mile to Km Converter")
window.minsize(width=250, height=120)
window.config(padx=20, pady=20)
Then we add the miles form input to the interface:
# Miles input
miles = Entry(width=8, justify="center")
miles.grid(column=1, row=0)
We use Tkinter's label
functionality to add all of our text to the interface:
# create label
label = Label(text="Miles", font=("Arial", 14))
label.grid(column=2, row=0)
# Is equal to label
iet_label = Label(text="is equal to", font=("Arial", 14))
iet_label.grid(column=0, row=1)
# Result label
res_label = Label(text="0", font=("Arial", 14))
res_label.grid(column=1, row=1)
# KM label
km_label = Label(text="Km", font=("Arial", 14))
km_label.grid(column=2, row=1)
And then we add the calculate
button that will trigger the conversion:
# Calculate button
button = Button(text="Calculate", command=calculate)
button.grid(column=1, row=2)
All of the commands below should be typed into the Python terminal of your IDE (I use PyCharm for my Python Development).
First, clone the repository from Github and switch to the new directory:
$ git clone git@github.com:shelbyblanton/miles-to-km-converter.git
Then open the project in PyCharm.
Setup is complete!
Click Run in PyCharm to see the app in action.
Programmed by M. Shelby Blanton under the instructional guidance of Dr. Angela Yu via Udemy.com.