Testing the tkinter libraries
King Mhar Bayato
Reliability Engineer @ Etihad | AMOS | Data Engineer | SQL | System Analyst
I'm on day 27 of my online course and just learned about arguments and kwargs. These concepts are beneficial for handling multiple or unlimited positional parameters in functions, which is fun. I didn't realize these concepts were already available in Python.
After that, the teacher introduced us to GUI programming. We started a simple project where you can click a button to convert a number from miles to kilometers. I used to work with Visual Basic, where you drag and drop objects, but in Python, everything is hardcoded. It’s a bit tedious, but also very enjoyable.
Here’s the code that demonstrates how to use the Tkinter library. I utilized different objects like labels, buttons, and entries, and used the grid method to place all my widgets:
You can copy the code below for educational purposes.
from tkinter import *
window = Tk()
window.title("Mile to KM Converter")
window.minsize(width=200, height=100)
entry = Entry(width=10)
entry.grid(column=1,row=0)
label1 = Label(text="is equal to")
label1.grid(column=0,row=1)
label2 = Label(text="Miles")
label2.grid(column=2,row=0)
label3 = Label(text="Km")
label3.grid(column=2,row=1)
converted_mile = Label(text="0")
converted_mile.grid(column=1,row=1)
def calculate_km():
x = int(entry.get())
x *= 1.609344
x = int(round(x,0))
converted_mile.config(text=x)
print(x)
button = Button(text="Calculate", command=calculate_km)
button.grid(column=1,row=2)
window.mainloop()