Converting Celsius to Fahrenheit in Python

Converting Celsius to Fahrenheit in Python

Introduction

In this tutorial we will create a Converting Celsius to Fahrenheit in Python. This tutorial purpose is to help you create a program that can convert the Celsius to Fahrenheit. This will cover all the basic parts for creating the conversion. I will provide a sample program to show the actual coding of this tutorial.

This tutorial will make it easy for you to understand just follow my instruction I provided and you can do it without a problem. This program is useful if you know the value of the temperature. I will try my best to give you the easiest way of creating this program Temperature Conversion. So let's do the coding.

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Importing Modules

After setting up the installation and the database, run the IDLE and click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python. Then copy code that I provided below and paste it inside the IDLE text editor.
  1. from tkinter import *

Setting up the Main Frame

After importing the modules, we will now then create the main frame for the application. To do that just copy the code below and paste it inside the IDLE text editor.
  1. root = Tk()
  2. root.title("Converting Celsius to Fahrenheit")
  3. width = 700
  4. height = 200
  5. screen_width = root.winfo_screenwidth()
  6. screen_height = root.winfo_screenheight()
  7. x = (screen_width/2) - (width/2)
  8. y = (screen_height/2) - (height/2)
  9. root.geometry("%dx%d+%d+%d" % (width, height, x, y))
  10. root.resizable(0, 0)
  11. root.config(bg="white")

Creating Main Function

This is the main function of the application is. This code will create the Temperature converter complete functionality. To do this just copy and write these block of codes inside the IDLE text editor.
  1. def CelciusToFahren():
  2. FAHRENHEIT.set("")
  3. if CELCIUS.get() != "":
  4. FAHRENHEIT.set((float(CELCIUS.get())*(9/5)) + 32)
  5.  
  6. def FahrenToCelcius():
  7. CELCIUS1.set("")
  8. if FAHRENHEIT1.get() != "":
  9. CELCIUS1.set((float(FAHRENHEIT1.get()) - 32) *( 5/9))
  10.  
  11.  
  12. CELCIUS = StringVar()
  13. CELCIUS1 = StringVar()
  14. FAHRENHEIT = StringVar()
  15. FAHRENHEIT1 = StringVar()

In the given code we created a two method for converting the temperature, Celsius to Fahrenheit and Fahrenheit to Celsius. To get the value Celsius to Fahrenheit we only use a simple formula using °F = °C × (9/5) + 32. And to get the Fahrenheit to Celsius value we use this formula °C = 5/9 x (F-32).

Designing the Layout

After creating the Functions we will now add some layout to the application. Just kindly copy the code below and paste it inside the IDLE text editor.
  1. Top = Frame(root, width=700, bd=1, relief=SOLID)
  2. Top.pack(side=TOP)
  3. BottomTitle = Frame(root, width=700, bd=1, relief=SOLID)
  4. BottomTitle.pack(side=TOP, pady=10)
  5. BottomForm = Frame(root, width=700, bg="white")
  6. BottomForm.pack(side=TOP)
  7. BottomLeft = Frame(BottomForm, width=250, bd=1, relief=SOLID)
  8. BottomLeft.pack(side=LEFT)
  9. BottomRight = Frame(BottomForm, width=250, bd=1, relief=SOLID)
  10. BottomRight.pack(side=RIGHT, padx=10)
  11.  
  12.  
  13.  
  14. lbl_title = Label(Top, text="Converting Celsius to Fahrenheit", width=700 ,font=('arial', 18))
  15. lbl_title.pack(fill=X)
  16. lbl3 = Label(BottomLeft, text="Celcius To Fahrenheit",font=('arial', 12))
  17. lbl3.grid(row=0, columnspan=5, column=0)
  18. lbl4 = Label(BottomRight, text="Fahrenheit To Celcius",font=('arial', 12))
  19. lbl4.grid(row=0, columnspan=5, column=0)
  20. lbl_celcius = Label(BottomLeft, text="Celcius", font=(12))
  21. lbl_celcius.grid(row=1, padx=5, pady=10)
  22. lbl_celcius1 = Label(BottomRight, text="Celcius", font=(12))
  23. lbl_celcius1.grid(row=1, column=3 )
  24. lbl_to2 = Label(BottomLeft, text="-")
  25. lbl_to2.grid(row=1, column=2)
  26. lbl_to3 = Label(BottomRight, text="-")
  27. lbl_to3.grid(row=1, column=2)
  28. lbl_fahrenheit = Label(BottomLeft, text="Fahrenheit", font=(12))
  29. lbl_fahrenheit.grid(row=1, column=3)
  30. lbl_fahrenheit1 = Label(BottomRight, text="Fahrenheit", font=(12))
  31. lbl_fahrenheit1.grid(row=1, padx=5, pady=10)
  32.  
  33.  
  34. celcius1 = Entry(BottomLeft, textvariable=CELCIUS, width=12)
  35. celcius1.grid(row=1, column=1)
  36. celcius2 = Entry(BottomRight, textvariable=CELCIUS1, width=12, state=DISABLED)
  37. celcius2.grid(row=1, column=4, padx=5)
  38. fahrenheit1 = Entry(BottomLeft, textvariable=FAHRENHEIT, width=12, state=DISABLED)
  39. fahrenheit1.grid(row=1, column=4, padx=5)
  40. fahrenheit2 = Entry(BottomRight, textvariable=FAHRENHEIT1, width=12)
  41. fahrenheit2.grid(row=1, column=1)
  42.  
  43.  
  44. btn3 = Button(BottomLeft, text="Convert", width=20, bg="white", command=CelciusToFahren)
  45. btn3.grid(row=2, columnspan=5, column=0, pady=10)
  46. btn4 = Button(BottomRight, text="Convert", width=20, bg="white", command=FahrenToCelcius)
  47. btn4.grid(row=2, columnspan=5, column=0, pady=10)

Initializing the Application

After finishing the function save the application as index.py. This function will run the code and check if the main is initialize properly. To do that copy and write these block code after the main function inside the IDLE text editor.

  1. if __name__ == '__main__':
  2. root.mainloop()

Output:



The Converting Celsius to Fahrenheit in Python source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created Converting Celsius to Fahrenheit in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials

Add new comment