Python - Simple Celcius To Fahrenheit Conversion

Submitted by: 
Language: 
Visitors have accessed this post 140 times.

In this tutorial we will create a Simple Celcius To Fahrenheit Conversion using Python. This code will convert the given value of celcius into fahrenheit when the convert button is clicked. The code use tkinter module to create a layout and widgets that can call python functions. When a function is called it the application will automatically convert the given celcius value into fahrenheit by calling CelciusToFahren() binded in the button widget.

We will be using Python because it has a design philosophy which emphasizes code readability. That's why python is very easy to use especially for beginners who just started programming. It is very easy to learn the syntax emphasizes readability and it can reduces time consuming in developing..

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("Python - Simple Celcius To Fahrenheit Conversion")
  3. width = 500
  4. height = 300
  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)

Designing the Layout

After creating the Main Frame we will now add some layout to the application. Just kindly copy the code below and paste it inside the IDLE text editor.

  1. #=======================VARIABLES======================
  2. CELCIUS = StringVar()
  3. FAHRENHEIT = StringVar()
  4.  
  5.  
  6. #=======================FRAME==========================
  7. Top = Frame(root, width=500, bd=1, relief=SOLID)
  8. Top.pack(side=TOP)
  9. Mid = Frame(root, width=500)
  10. Mid.pack(side=TOP)
  11. Form = Frame(Mid, width=250, bd=1, relief=SOLID)
  12. Form.pack(side=TOP, pady=50)
  13.  
  14.  
  15. #=======================LABEL WIDGET===================
  16. lbl_title = Label(Top, text="Python - Simple Celcius To Fahrenheit Conversion", width=500 ,font=('arial', 14))
  17. lbl_title.pack(fill=X)
  18. lbl = Label(Form, text="Celcius To Fahrenheit",font=('arial', 12))
  19. lbl.grid(row=0, columnspan=5, column=0)
  20. lbl_celcius = Label(Form, text="Celcius", font=(12))
  21. lbl_celcius.grid(row=1, padx=5, pady=10)
  22. lbl_fahrenheit = Label(Form, text="Fahrenheit", font=(12))
  23. lbl_fahrenheit.grid(row=1, column=3)
  24.  
  25. #=======================ENTRY WIDGET===================
  26. celcius = Entry(Form, textvariable=CELCIUS, width=12)
  27. celcius.grid(row=1, column=1)
  28. fahrenheit = Entry(Form, textvariable=FAHRENHEIT, width=12, state=DISABLED)
  29. fahrenheit.grid(row=1, column=4, padx=5)
  30.  
  31. #=======================BUTTON WIDGET==================
  32. btn_convert = Button(Form, text="Convert", width=20, command=CelciusToFahren)
  33. btn_convert.grid(row=2, columnspan=5, column=0, pady=10)

Creating the Main Function

This is where the code that contains the main functions. This code will convert the celcius value into fahrenheit when the button is clicked. To do that just copy and write these blocks of code.

  1. #=======================METHODS========================
  2. def CelciusToFahren():
  3. FAHRENHEIT.set("")
  4. if CELCIUS.get() != "":
  5. FAHRENHEIT.set((float(CELCIUS.get())*(9/5)) + 32)

Initializing the Application

After finishing the function save the application as index.py. This function will run the code and check if the main application is initialized properly. To do that copy the code below and paste it inside the IDLE text editor.

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

There you have it we just created a Simple Celcius To Fahrenheit Conversion Using Python. I hope that this simple tutorial help you for what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.