Home » Celsius to Fahrenheit Converter Using Python Tkinter

Celsius to Fahrenheit Converter Using Python Tkinter

celsius to fahrenheit converter using tkinter python

Today in this blog post, we are going to develop an application that can convert temperature in Celsius to Fahrenheit using python Tkinter GUI library. I think it’s a very basic and easy Tkinter project idea to build.

Let’s start building this application, firstly I have created a simple user interface that looks like in the video below.

I have placed an entry box to type in the temperature in celsius and a button labeled convert. Here is the code that I have written to develop this interface.

from tkinter import *
import tkinter.font as font

my_window = Tk()
my_window.title("Celsius to Fahrenheit Converter")

#Displaying heading inside window
description = Label(text = 'Celsius -> Fahrenheit', font = font.Font(size = 20), fg = "grey")
description.pack()

frame = Frame(my_window)
frame.pack(pady = 20)

#entry box to enter temperature in celsius
message_one = Label(frame, text = 'Enter Temperature in Celsius : ', font = font.Font(size = 10))
message_one.grid(row = 0, column = 0)

celsius_value = Entry(frame)
celsius_value.grid(row = 0, column = 1)

#To Display Error Message
error_msg = Label(frame, text = 'Please enter valid input...', font = font.Font(size = 8), fg = 'red')

#To Display the Output
output_fahrenheit = Label(frame, font = font.Font(size = 12))
output_fahrenheit.grid(row = 2, column = 0, columnspan = 2, pady = 10)

#Submit Button
submit_btn = Button(frame, text = 'Convert', width = 30, fg = "black", bg = "light green", bd = 0, padx = 20, pady = 10, command = convert)
submit_btn.grid(row = 3, column = 0, columnspan = 2, pady = 10)

my_window.geometry('500x250')
my_window.mainloop()

When the user clicks on the convert button after typing in the temperature value in Celsius, the convert() function will be called.

#Function to Convert Celsius to Fahrenheit
def convert():
    temp_celsius = celsius_value.get()

    if(temp_celsius.replace('.','',1).isdigit()):
        error_msg.grid_forget()
        temp_fahrenheit = (float(temp_celsius) * 9/5) + 32
        output_fahrenheit.config(text = 'Temperature in Fahrenheit : ' + str(temp_fahrenheit))
    else:
        error_msg.grid(row=1, column=1)

The convert() function, we will first get the value that the user entered and checks whether the entered value is number or not. If the entered value is not a number, then we will throw an error saying “please enter valid input”.

Also check out:

If the user enters the valid input, then we are going to convert the Celsius value into equivalent Fahrenheit value using the formula. 

(ENTERED_VALUE * (9/5)) + 3

ENTERED_VALUE will be the user entered celsius value.

And the final output temperature in Fahrenheit will be displayed on the screen.

Conclusion

We have built this simple Celsius to Fahrenheit converter application using python Tkinter and yes it’s a very basic application. We can also build a large application that can perform all kinds of conversions.

Leave a Reply

Your email address will not be published.