-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTemperatureConverter.py
37 lines (31 loc) · 1.21 KB
/
TemperatureConverter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import tkinter as tk
# Root window
window = tk.Tk()
# Labels
user_input_label = tk.Label(text="Please enter the temperature you wish to convert")
user_input_label.pack()
user_input_field = tk.Entry(window)
user_input_field.pack()
# A functin that converts celcius to farenheit and vice-versa
# Updates the GUI such that the result is displayed in a label element
def temperature_converter():
user_input = user_input_field.get().lower()
try:
if "f" in user_input:
temp_f = float(user_input.replace("f", ""))
temp_c = ((temp_f - 32) * 5) / 9
result = tk.Label(text=f"Result:- {round(temp_c, 3)}°C")
result.pack()
elif "c" in user_input:
temp_c = float(user_input.replace("c", ""))
temp_f = (1.8 * temp_c) + 32
result = tk.Label(text=f"Result:- {round(temp_f, 3)}°F")
result.pack()
else:
print("Make sure to include 'c' for celsius or 'f' for farenheit at the end of the input")
print("bruh")
except:
print("Error occured, please re-run program")
submit_button = tk.Button(text="Submit", command = temperature_converter)
submit_button.pack()
window.mainloop()