Python Tkinter: Celsius to Fahrenheit Converter

Feature Img Celsius 2 Fahrenheit

Hello fellow programmer! Today we are going to build our own application to convert Celsius to Fahrenheit using Python tkinter.

Building a Celsius to Fahrenheit Converter using Tkinter

The topic is self explanatory, hence we directly move to the implementation part.

Step 1: Creating a window

Before we begin with the main parts of the project we have to create a blank customized window. The code for the same is shown below:

import tkinter as tk
window = tk.Tk()
window.geometry("300x250")
window.config(bg="#A569BD")
window.resizable(width=False,height=False)
window.title('Celsius to Fahrenheit Converter!')

We make sure to do the following things in this step:

  1. Import tkinter module
  2. Creating a window object and setting the dimensions and background color of the window
  3. Set the resizable attributes as false to make sure the dimensions mentioned stay fixed.
  4. Setting the title of the application

The result of the code mentioned above is as follows:

Blank Screen Tkinter Celsius to Fahrenheit
Blank Screen Tkinter

Step 2: Adding the basic elements to the screen

The basic elements used for the application as listed below:

  1. Two Labels: Information about the application
  2. Entry box: Take input of the temperature value
  3. Button_1: To convert the temperature to Fahrenheit
  4. Text Box: To show the result of the converted temperature
  5. Button_2: To exit the application

The code to add all the elements is shown below:

l1 = tk.Label(window,text="Celsius to Fahrenheit Converter",font=("Arial", 15),fg="white",bg="black")
l2= tk.Label(window,text="Enter temperature in Celsius: ",font=("Arial", 10,"bold"),fg="white",bg="#A569BD")
l3= tk.Label(window,text="Temperature in Fahrenheit is: ",font=("Arial", 10,"bold"),fg="white",bg="#A569BD")

empty_l1 = tk.Label(window,bg="#A569BD")
empty_l2 = tk.Label(window,bg="#A569BD")

e1= tk.Entry(window,font=('Arial',10))

btn1 = tk.Button(window,text="Convert to Fahrenheit!",font=("Arial", 10))
btn2 = tk.Button(window,text="Exit application",font=("Arial", 10))

t1=tk.Text(window,state="disabled",width=15,height=0)

In addition to all the mentioned elements, we make sure we have some empty labels to add new line between two elements.

For adding the elements on the screen we make use of the pack function and pack all elements in a particular order. Packing code is shown below:

l1.pack()
l2.pack()
e1.pack()
empty_l1.pack()
btn1.pack()
l3.pack()
t1.pack()
empty_l2.pack()
btn2.pack()

The output looks something like what’s shown below:

Initial Image Celsius to Fahrenheit
Initial Image

Step 3: Adding Functionalities to the Buttons

Button 1: Exit Application Button

Let us create a simpler button first to exit the application. All we need to do is create a function which will destroy the window. The code for the same is shown below:

def exit():
    window.destroy()

To execute the function on the button click, we add the command attribute to the exit application button. And add the command as the exit function.

Button 2: Convert Temperature Button

The second button converts the temperature to ‘Fahrenheit’ and the same involves the following steps:

  1. Get the Celsius temperature from the entry box
  2. Use the formula to convert the temperature obtained to Fahrenheit temperature
  3. Clear the textbox
  4. Insert the value of the temperature calculated into the textbox

The code for the same is shown below:

def convert():
    c = int(e1.get())
    f = ((c*9)/(5))+32
    t1.config(state='normal')
    t1.delete('1.0', tk.END)
    t1.insert(tk.END,f)
    t1.config(state='disabled')

To make the function, executable on the button click add the command attribute to the convert button.

Complete Code for Celsius to Farhenheit Converter

def exit():
    window.destroy()

def convert():
    c = int(e1.get())
    f = ((c*9)/(5))+32
    t1.config(state='normal')
    t1.delete('1.0', tk.END)
    t1.insert(tk.END,f)
    t1.config(state='disabled')

import tkinter as tk
window = tk.Tk()
window.geometry("300x250")
window.config(bg="#A569BD")
window.resizable(width=False,height=False)
window.title('Celsius to Fahrenheit Converter!')

l1 = tk.Label(window,text="Celsius to Fahrenheit Converter",font=("Arial", 15),fg="white",bg="black")
l2= tk.Label(window,text="Enter temperature in Celsius: ",font=("Arial", 10,"bold"),fg="white",bg="#A569BD")
l3= tk.Label(window,text="Temperature in Fahrenheit is: ",font=("Arial", 10,"bold"),fg="white",bg="#A569BD")

empty_l1 = tk.Label(window,bg="#A569BD")
empty_l2 = tk.Label(window,bg="#A569BD")

e1= tk.Entry(window,font=('Arial',10))

btn1 = tk.Button(window,text="Convert to Fahrenheit!",font=("Arial", 10),command=convert)
btn2 = tk.Button(window,text="Exit application",font=("Arial", 10),command=exit)

t1=tk.Text(window,state="disabled",width=15,height=0)

l1.pack()
l2.pack()
e1.pack()
empty_l1.pack()
btn1.pack()
l3.pack()
t1.pack()
empty_l2.pack()
btn2.pack()

window.mainloop()

Sample Outputs

The application above was tested for two test samples and the results were perfect! The output screes for both the inputs are shown below:

Output1 C2F Celsius to Fahrenheit Tkinter
Output1 C2F
Output2 C2F Celsius to Fahrenheit
Output2 C2F

Conclusion

Congratulations! You have successfully built an application that converts Celsius temperature to Fahrenheit temperature.

Hope you liked the project! Keep coding!