Level 3: Real-World
I did mine, here is the code:
rom tkinter import Tk, Label, Entry, Button
# Conversion factor
conversion_rate = 0.621371
def convert():
"""
Retrieves user input, converts kilometers to miles, and displays the result.
:return:
# Get kilometers value
try:
km_value = float(km_entry.get())
except ValueError:
# Handle invalid input
result_label.config(text="Invalid input. Please enter a number.")
return
# Calculate miles
miles_value = km_value * conversion_rate
# Display result with 2 decimal places
result_label.config(text=f"Distance in miles: {miles_value:.2f}")
# Create main window
window = Tk()
window.title("Km to Miles Converter")
# Label for kilometers entry
km_label = Label(window, text="Enter distance in kilometers:")
km_label.grid(row=0, column=0, pady=10)
# Entry field for kilometers
km_entry = Entry(window)
km_entry.grid(row=0, column=1, pady=10)
# Button to trigger conversion
convert_button = Button(window, text="Convert", command=convert)
convert_button.grid(row=1, columnspan=2, pady=10)
# Label to display conversion result
result_label = Label(window, text="")
result_label.grid(row=2, columnspan=2)
window.mainloop()
Very Interesting!
I did mine using PySimpleGui
Great! Feel free to post the code here. ✔️
I have posted the code but removed it. Because it was a mess.
Is there a way for it to show code properly in the comments?
But I added it to pastebin, so perhaps it can be read from there
https://pastebin.com/RDZeQD5W
Pastebin works too. Good job!
I did mine, here is the code:
rom tkinter import Tk, Label, Entry, Button
# Conversion factor
conversion_rate = 0.621371
def convert():
"""
Retrieves user input, converts kilometers to miles, and displays the result.
:return:
"""
# Get kilometers value
try:
km_value = float(km_entry.get())
except ValueError:
# Handle invalid input
result_label.config(text="Invalid input. Please enter a number.")
return
# Calculate miles
miles_value = km_value * conversion_rate
# Display result with 2 decimal places
result_label.config(text=f"Distance in miles: {miles_value:.2f}")
# Create main window
window = Tk()
window.title("Km to Miles Converter")
# Label for kilometers entry
km_label = Label(window, text="Enter distance in kilometers:")
km_label.grid(row=0, column=0, pady=10)
# Entry field for kilometers
km_entry = Entry(window)
km_entry.grid(row=0, column=1, pady=10)
# Button to trigger conversion
convert_button = Button(window, text="Convert", command=convert)
convert_button.grid(row=1, columnspan=2, pady=10)
# Label to display conversion result
result_label = Label(window, text="")
result_label.grid(row=2, columnspan=2)
window.mainloop()
Very Interesting!
I did mine using PySimpleGui
Great! Feel free to post the code here. ✔️
I have posted the code but removed it. Because it was a mess.
Is there a way for it to show code properly in the comments?
But I added it to pastebin, so perhaps it can be read from there
https://pastebin.com/RDZeQD5W
Pastebin works too. Good job!