Python: Simple Temperature Conversion

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

In this tutorial we will create a Python: Simple Temperature Conversion Using Python. Python has a design philosophy which emphasizes code readability. Python is very easy to learn the syntax emphasizes readability and it can reduces time consuming in developing. You can use this to convert the units value. So let's now 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 the 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("Sourcecodester")
  3. width = 700
  4. height = 400
  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="#ff9999")

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. METER = StringVar()
  3. METER1 = StringVar()
  4. CENTIMETER = StringVar()
  5. CENTIMETER1 = StringVar()
  6. CELCIUS = StringVar()
  7. CELCIUS1 = StringVar()
  8. FAHRENHEIT = StringVar()
  9. FAHRENHEIT1 = StringVar()
  10.  
  11. #=======================FRAME==========================
  12. Top = Frame(root, width=700, bd=1, relief=SOLID)
  13. Top.pack(side=TOP)
  14. TopTitle = Frame(root, width=700, bd=1, relief=SOLID)
  15. TopTitle.pack(side=TOP, pady=10)
  16. TopForm = Frame(root, width=700, bg="#ff9999")
  17. TopForm.pack(side=TOP)
  18. TopLeft = Frame(TopForm, width=350, bd=1, relief=SOLID)
  19. TopLeft.pack(side=LEFT, padx=10)
  20. TopRight = Frame(TopForm, width=350, bd=1, relief=SOLID)
  21. TopRight.pack(side=RIGHT, padx=10)
  22. BottomTitle = Frame(root, width=700, bd=1, relief=SOLID)
  23. BottomTitle.pack(side=TOP, pady=10)
  24. BottomForm = Frame(root, width=700, bg="#ff9999")
  25. BottomForm.pack(side=TOP)
  26. BottomLeft = Frame(BottomForm, width=250, bd=1, relief=SOLID)
  27. BottomLeft.pack(side=LEFT)
  28. BottomRight = Frame(BottomForm, width=250, bd=1, relief=SOLID)
  29. BottomRight.pack(side=RIGHT, padx=10)
  30.  
  31.  
  32. #=======================LABEL WIDGET===================
  33. lbl_title = Label(Top, text="Python: Simple Temperature Conversion", width=700 ,font=('arial', 18))
  34. lbl_title.pack(fill=X)
  35. lbl_TopTitle = Label(TopTitle, text="Metric", width=700 ,font=('arial', 12), bg="#ccccff")
  36. lbl_TopTitle.pack(fill=X)
  37. lbl_BottomTitle = Label(BottomTitle, text="Temperature", width=700 ,font=('arial', 12), bg="#ccccff")
  38. lbl_BottomTitle.pack(fill=X)
  39. lbl1 = Label(TopLeft, text="Meter To Centimeter",font=('arial', 12))
  40. lbl1.grid(row=0, columnspan=5, column=0)
  41. lbl2 = Label(TopRight, text="Centimeter To Meter",font=('arial', 12))
  42. lbl2.grid(row=0, columnspan=5, column=0)
  43. lbl3 = Label(BottomLeft, text="Celcius To Fahrenheit",font=('arial', 12))
  44. lbl3.grid(row=0, columnspan=5, column=0)
  45. lbl4 = Label(BottomRight, text="Farenheit To Celcius",font=('arial', 12))
  46. lbl4.grid(row=0, columnspan=5, column=0)
  47. lbl_meter = Label(TopLeft, text="Meter", font=(12))
  48. lbl_meter.grid(row=1, padx=5, pady=10)
  49. lbl_meter1 = Label(TopRight, text="Meter", font=(12))
  50. lbl_meter1.grid(row=1, column=3, padx=5, pady=10)
  51. lbl_to = Label(TopLeft, text="-")
  52. lbl_to.grid(row=1, column=2)
  53. lbl_to1 = Label(TopRight, text="-")
  54. lbl_to1.grid(row=1, column=2)
  55. lbl_centimeter = Label(TopLeft, text="Centimeter", font=(12))
  56. lbl_centimeter.grid(row=1, column=3)
  57. lbl_centimeter1 = Label(TopRight, text="Centimeter", font=(12))
  58. lbl_centimeter1.grid(row=1)
  59. lbl_celcius = Label(BottomLeft, text="Celcius", font=(12))
  60. lbl_celcius.grid(row=1, padx=5, pady=10)
  61. lbl_celcius1 = Label(BottomRight, text="Celcius", font=(12))
  62. lbl_celcius1.grid(row=1, column=3 )
  63. lbl_to2 = Label(BottomLeft, text="-")
  64. lbl_to2.grid(row=1, column=2)
  65. lbl_to3 = Label(BottomRight, text="-")
  66. lbl_to3.grid(row=1, column=2)
  67. lbl_fahrenheit = Label(BottomLeft, text="Fahrenheit", font=(12))
  68. lbl_fahrenheit.grid(row=1, column=3)
  69. lbl_fahrenheit1 = Label(BottomRight, text="Fahrenheit", font=(12))
  70. lbl_fahrenheit1.grid(row=1, padx=5, pady=10)
  71.  
  72. #=======================ENTRY WIDGET===================
  73. meter1 = Entry(TopLeft, textvariable=METER, width=12)
  74. meter1.grid(row=1, column=1)
  75. meter2 = Entry(TopRight, textvariable=METER1, width=12, state=DISABLED)
  76. meter2.grid(row=1, column=4, padx=5)
  77. centimeter1 = Entry(TopLeft, textvariable=CENTIMETER, width=12, state=DISABLED)
  78. centimeter1.grid(row=1, column=4, padx=5)
  79. centimeter2 = Entry(TopRight, textvariable=CENTIMETER1, width=12)
  80. centimeter2.grid(row=1, column=1)
  81. celcius1 = Entry(BottomLeft, textvariable=CELCIUS, width=12)
  82. celcius1.grid(row=1, column=1)
  83. celcius2 = Entry(BottomRight, textvariable=CELCIUS1, width=12, state=DISABLED)
  84. celcius2.grid(row=1, column=4, padx=5)
  85. fahrenheit1 = Entry(BottomLeft, textvariable=FAHRENHEIT, width=12, state=DISABLED)
  86. fahrenheit1.grid(row=1, column=4, padx=5)
  87. fahrenheit2 = Entry(BottomRight, textvariable=FAHRENHEIT1, width=12)
  88. fahrenheit2.grid(row=1, column=1)
  89.  
  90. #=======================BUTTON WIDGET==================
  91. btn1 = Button(TopLeft, text="Convert", width=20, bg="#006dcc", command=MeterToCenti)
  92. btn1.grid(row=2, columnspan=5, column=0, pady=10)
  93. btn2 = Button(TopRight, text="Convert", width=20, bg="#006dcc", command=CentiToMeter)
  94. btn2.grid(row=2, columnspan=5, column=0, pady=10)
  95. btn3 = Button(BottomLeft, text="Convert", width=20, bg="#006dcc", command=CelciusToFahren)
  96. btn3.grid(row=2, columnspan=5, column=0, pady=10)
  97. btn4 = Button(BottomRight, text="Convert", width=20, bg="#006dcc", command=FahrenToCelcius)
  98. btn4.grid(row=2, columnspan=5, column=0, pady=10)

Creating the Main Function
This is the main function where the conversion is being processed. To do that just simply copy the code below then paste it inside the IDLE text editor

  1. #=======================METHODS========================
  2. def MeterToCenti():
  3. CENTIMETER.set("")
  4. if METER.get() != "":
  5. CENTIMETER.set(float(METER.get()) * 100)
  6.  
  7. def CentiToMeter():
  8. METER1.set("")
  9. if CENTIMETER1.get() != "":
  10. METER1.set(float(CENTIMETER1.get()) / 100)
  11.  
  12. def CelciusToFahren():
  13. FAHRENHEIT.set("")
  14. if CELCIUS.get() != "":
  15. FAHRENHEIT.set((float(CELCIUS.get())*(9/5)) + 32)
  16.  
  17. def FahrenToCelcius():
  18. CELCIUS1.set("")
  19. if FAHRENHEIT1.get() != "":
  20. CELCIUS1.set((float(FAHRENHEIT1.get()) - 32) *( 5/9))
  21.  

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 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 Temperature Conversion Using Python. I hope that this simple tutorial help you expand your idea about Python programming. For more updates and tutorials just kindly visit this site. Enjoy Coding!!


Tags: 

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe>
  • You may insert videos with [video:URL]
  • 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.