-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathAdded_temperature_convertor_GUI_python_script.py
45 lines (36 loc) · 1.34 KB
/
Added_temperature_convertor_GUI_python_script.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
38
39
40
41
42
43
44
45
import tkinter as tk
def convert_temperature():
try:
temperature = float(entry.get())
if var.get() == 0: # Celsius to Fahrenheit
result = temperature * 9/5 + 32
output_label.configure(text=f"{temperature}°C = {result}°F")
elif var.get() == 1: # Fahrenheit to Celsius
result = (temperature - 32) * 5/9
output_label.configure(text=f"{temperature}°F = {result}°C")
except ValueError:
output_label.configure(text="Invalid input")
# Create the main window
window = tk.Tk()
window.title("Temperature Converter")
# Create input label and entry widget
input_label = tk.Label(window, text="Enter temperature:")
input_label.pack()
entry = tk.Entry(window)
entry.pack()
# Create radio buttons for temperature conversion options
var = tk.IntVar()
celsius_to_fahrenheit = tk.Radiobutton(
window, text="Celsius to Fahrenheit", variable=var, value=0)
celsius_to_fahrenheit.pack()
fahrenheit_to_celsius = tk.Radiobutton(
window, text="Fahrenheit to Celsius", variable=var, value=1)
fahrenheit_to_celsius.pack()
# Create convert button
convert_button = tk.Button(window, text="Convert", command=convert_temperature)
convert_button.pack()
# Create output label for displaying result
output_label = tk.Label(window)
output_label.pack()
# Run the main event loop
window.mainloop()