Home » Temperature converter in python

Temperature converter in python

Oracle Java Certification
Spring Framework Basics Video Course
1 Year Subscription
Java SE 11 Programmer II [1Z0-816] Practice Tests
Java SE 11 Programmer I [1Z0-815] Practice Tests
Java SE 11 Developer (Upgrade) [1Z0-817]

In this example we will create a temperature converter in python

Here are the formulas for temperature conversion

Fahrenheit to Celsius formula:

(°F – 32) x 5/9 = °C or in plain english,

Celsius to Fahrenheit formula:

(°C × 9/5) + 32 = °F

Code

basic example

#!/usr/bin/env python

def convert(temperature, unit):
    unit = unit.lower()
    if unit == "c":
        temperature = 9.0 / 5.0 * temperature + 32
        return "%s degrees Fahrenheit"% temperature
    if unit == "f":
        temperature = (temperature - 32)  / 9.0 * 5.0
        return "%s degrees Celsius"% temperature
 
inptemp = int(input("What is the temperature?\n"))
inpunit = str(input("Please enter the unit of measure (f or c):\n"))
 
print (convert(inptemp, inpunit))

Output

Tkinter example

from tkinter import *
from tkinter import messagebox

def cal():
    try:
        inputCelsius = float(TextBox1.get("1.0", "end-1c"))
        fahrenheit = (inputCelsius * 1.8) + 32
        TextBox2.insert(INSERT,fahrenheit)
    except:
        messagebox.showerror("Error","Please enter a integer/float value")

def calF():
    try:
        inputFah = float(TextBox3.get("1.0", "end-1c"))
        Celsius = (inputFah - 32) * 5/9
        TextBox4.insert(INSERT,Celsius)
    except:
        messagebox.showerror("Error","Please enter a integer/float value")

def clr():
    TextBox1.delete('1.0', END)
    TextBox2.delete('1.0', END)
    TextBox3.delete('1.0', END)
    TextBox4.delete('1.0', END)

root = Tk()
root.title("Temperature Converter")
root.geometry("400x500")
root.resizable(width=FALSE, height=False)

label1 = Label(root, text="\nEnter Temperature in Celsius")
label1.grid(row=3, column=3)
TextBox1 = Text(root, height=2, width=7,font=(30))
TextBox1.grid(row=4, column=3, sticky=N)

b1 = Button(compound=LEFT, text="Submit", foreground="blue",command=cal)
b1.grid(row=5,column=3)
label2 = Label(root, text="\nTemperature in Fahrenheit")
label2.grid(row=6, column=3)
TextBox2 = Text(root, height=2, width=7,font=(30))
TextBox2.grid(row=7, column=3, sticky=N)

label3= Label(root,text="-------------------------------------")
label3.grid(row=8,column=3)

label1 = Label(root, text="\nEnter Temperature in Fahrenheit")
label1.grid(row=9, column=3)
TextBox3 = Text(root, height=2, width=7,font=(30))
TextBox3.grid(row=10, column=3, sticky=N)

b2 = Button(compound=LEFT, text="Submit", foreground="blue",command=calF)
b2.grid(row=11,column=3)
label4 = Label(root, text="\nTemperature in Celsius")
label4.grid(row=12, column=3)
TextBox4 = Text(root, height=2, width=7,font=(30))
TextBox4.grid(row=13, column=3, sticky=N)
label5 = Label(root, text="")
label5.grid(row=14, column=3)
b3 = Button(compound=LEFT, text="Clear TextBox", fg="white",height=2,width=12,background="black",borderwidth=2, relief="groove",command=clr)
b3.grid(row=15,column=3)

label2 = Label(root, font=("Arial", 28), text="Temperature Converter")
label2.grid(row=1, column=3)

root.mainloop()

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More