PYTHON PROJECT
TEMPERATURE CONVERSION GUI USING PYTHON PROGRAM
NAME : AMBIK DEB
SEC : L
ROLL : 18
STREAM: ECE
B.TECH 1ST YEAR 1ST SEMISTER
import tkinter as tk
def convert_temperature():
try:
temperature = float(entry.get())
except ValueError:
result_label.config(text="Invalid input", fg="red")
return
if var.get() == 1: # Celsius to Kelvin
result = temperature + 273.15
elif var.get() == 2: # Celsius to Fahrenheit
result = (temperature * 9/5) + 32
elif var.get() == 3: # Kelvin to Celsius
result = temperature - 273.15
elif var.get() == 4: # Kelvin to Fahrenheit
result = (temperature - 273.15) * 9/5 + 32
elif var.get() == 5: # Fahrenheit to Celsius
result = (temperature - 32) * 5/9
else: # Fahrenheit to Kelvin
result = (temperature - 32) * 5/9 + 273.15
result_label.config(text=f"Result: {result:.2f}", fg="white")
# Create the main window
root = tk.Tk()
root.title("Temperature Converter")
root.configure(bg="black")
window_width = 500
window_height = 400
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x_positon = (screen_width - window_width) // 2
y_positon = (screen_height - window_height) // 2
root.geometry(f"{window_width}x{window_height}+{x_positon}+
{y_positon}”)
frame = tk.Frame(root, bg="black")
frame.place(relx=0.5, rely=0.5, anchor="center")
label = tk.Label(frame, text="Enter Temperature:", fg="white", bg="black")
label.grid(row=0, column=0, padx=10, pady=10)
entry = tk.Entry(frame, width=15)
entry.grid(row=0, column=1, padx=10, pady=10)
var = tk.IntVar()
var.set(1)
options = [
(“Celsius to Kelvin”, 1),
(“Celsius to Fahrenheit”, 2),
(“Kelvin to Celsius”, 3),
(“Kelvin to Fahrenheit”, 4),
(“Fahrenheit to Celsius”, 5),
(“Fahrenheit to Kelvin”, 6)
For i,(text,value)enumerate
(options):
Tk.RadioButton(frame,
text=text, variable=var,
value=value, bg=”black”,
Fg=”white”,
Selectcolor=”black”,
activebackground=”black”,
activeforeground=”white”).grid
(row=i+1, column=0,
columnspan=2, pady=5)
convert_button =
tk.button(frame,
text=”Convert”,
command=convert_temperatur
e, bg=”green”, fg=”white”)
convert_button.grid(row=len( o
ptions)+1, column=0,
columnspan=2, pady=10)
result_label = tk.Label(frame,
text=”Result after conversion:
“, fg=”white”,
bg=”black”)
result_label.grid(row=len(optio
ns)+2, column=0,
columnspan=2, pady=10)
root.mainloop()
OUTPUT OF THE PROGRAM
RESULT :
This code creates a simple temperature converter with a graphical user
interface using the tkinter library. It uses radio buttons for the user to
select the input and output temperature units. The UI elements are
styled with the specified colors, and the conversion result is displayed
in a white text color.
By creating this project, I've learned how to:
1}Use the tkinter library for building GUI applications in Python.
2}Handle user input and perform basic error checking.
3}Dynamically update the GUI based on user interaction
4}Apply styles and forma ng to different UI elements.
This project combines elements of user interface design, event
handling, and basic arithmetic operations, providing a practical
example for learning GUI programming with Python.