Python: Simple Temperature Conversion
Submitted by razormist on Thursday, May 11, 2017 - 13:43.
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
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.
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.
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
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.
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!!
- from tkinter import *
- root = Tk()
- root.title("Sourcecodester")
- width = 700
- height = 400
- screen_width = root.winfo_screenwidth()
- screen_height = root.winfo_screenheight()
- x = (screen_width/2) - (width/2)
- y = (screen_height/2) - (height/2)
- root.geometry("%dx%d+%d+%d" % (width, height, x, y))
- root.resizable(0, 0)
- root.config(bg="#ff9999")
- #=======================VARIABLES======================
- METER = StringVar()
- METER1 = StringVar()
- CENTIMETER = StringVar()
- CENTIMETER1 = StringVar()
- CELCIUS = StringVar()
- CELCIUS1 = StringVar()
- FAHRENHEIT = StringVar()
- FAHRENHEIT1 = StringVar()
- #=======================FRAME==========================
- Top = Frame(root, width=700, bd=1, relief=SOLID)
- Top.pack(side=TOP)
- TopTitle = Frame(root, width=700, bd=1, relief=SOLID)
- TopTitle.pack(side=TOP, pady=10)
- TopForm = Frame(root, width=700, bg="#ff9999")
- TopForm.pack(side=TOP)
- TopLeft = Frame(TopForm, width=350, bd=1, relief=SOLID)
- TopLeft.pack(side=LEFT, padx=10)
- TopRight = Frame(TopForm, width=350, bd=1, relief=SOLID)
- TopRight.pack(side=RIGHT, padx=10)
- BottomTitle = Frame(root, width=700, bd=1, relief=SOLID)
- BottomTitle.pack(side=TOP, pady=10)
- BottomForm = Frame(root, width=700, bg="#ff9999")
- BottomForm.pack(side=TOP)
- BottomLeft = Frame(BottomForm, width=250, bd=1, relief=SOLID)
- BottomLeft.pack(side=LEFT)
- BottomRight = Frame(BottomForm, width=250, bd=1, relief=SOLID)
- BottomRight.pack(side=RIGHT, padx=10)
- #=======================LABEL WIDGET===================
- lbl_title = Label(Top, text="Python: Simple Temperature Conversion", width=700 ,font=('arial', 18))
- lbl_title.pack(fill=X)
- lbl_TopTitle = Label(TopTitle, text="Metric", width=700 ,font=('arial', 12), bg="#ccccff")
- lbl_TopTitle.pack(fill=X)
- lbl_BottomTitle = Label(BottomTitle, text="Temperature", width=700 ,font=('arial', 12), bg="#ccccff")
- lbl_BottomTitle.pack(fill=X)
- lbl1 = Label(TopLeft, text="Meter To Centimeter",font=('arial', 12))
- lbl1.grid(row=0, columnspan=5, column=0)
- lbl2 = Label(TopRight, text="Centimeter To Meter",font=('arial', 12))
- lbl2.grid(row=0, columnspan=5, column=0)
- lbl3 = Label(BottomLeft, text="Celcius To Fahrenheit",font=('arial', 12))
- lbl3.grid(row=0, columnspan=5, column=0)
- lbl4 = Label(BottomRight, text="Farenheit To Celcius",font=('arial', 12))
- lbl4.grid(row=0, columnspan=5, column=0)
- lbl_meter = Label(TopLeft, text="Meter", font=(12))
- lbl_meter.grid(row=1, padx=5, pady=10)
- lbl_meter1 = Label(TopRight, text="Meter", font=(12))
- lbl_meter1.grid(row=1, column=3, padx=5, pady=10)
- lbl_to = Label(TopLeft, text="-")
- lbl_to.grid(row=1, column=2)
- lbl_to1 = Label(TopRight, text="-")
- lbl_to1.grid(row=1, column=2)
- lbl_centimeter = Label(TopLeft, text="Centimeter", font=(12))
- lbl_centimeter.grid(row=1, column=3)
- lbl_centimeter1 = Label(TopRight, text="Centimeter", font=(12))
- lbl_centimeter1.grid(row=1)
- lbl_celcius = Label(BottomLeft, text="Celcius", font=(12))
- lbl_celcius.grid(row=1, padx=5, pady=10)
- lbl_celcius1 = Label(BottomRight, text="Celcius", font=(12))
- lbl_celcius1.grid(row=1, column=3 )
- lbl_to2 = Label(BottomLeft, text="-")
- lbl_to2.grid(row=1, column=2)
- lbl_to3 = Label(BottomRight, text="-")
- lbl_to3.grid(row=1, column=2)
- lbl_fahrenheit = Label(BottomLeft, text="Fahrenheit", font=(12))
- lbl_fahrenheit.grid(row=1, column=3)
- lbl_fahrenheit1 = Label(BottomRight, text="Fahrenheit", font=(12))
- lbl_fahrenheit1.grid(row=1, padx=5, pady=10)
- #=======================ENTRY WIDGET===================
- meter1 = Entry(TopLeft, textvariable=METER, width=12)
- meter1.grid(row=1, column=1)
- meter2 = Entry(TopRight, textvariable=METER1, width=12, state=DISABLED)
- meter2.grid(row=1, column=4, padx=5)
- centimeter1 = Entry(TopLeft, textvariable=CENTIMETER, width=12, state=DISABLED)
- centimeter1.grid(row=1, column=4, padx=5)
- centimeter2 = Entry(TopRight, textvariable=CENTIMETER1, width=12)
- centimeter2.grid(row=1, column=1)
- celcius1 = Entry(BottomLeft, textvariable=CELCIUS, width=12)
- celcius1.grid(row=1, column=1)
- celcius2 = Entry(BottomRight, textvariable=CELCIUS1, width=12, state=DISABLED)
- celcius2.grid(row=1, column=4, padx=5)
- fahrenheit1 = Entry(BottomLeft, textvariable=FAHRENHEIT, width=12, state=DISABLED)
- fahrenheit1.grid(row=1, column=4, padx=5)
- fahrenheit2 = Entry(BottomRight, textvariable=FAHRENHEIT1, width=12)
- fahrenheit2.grid(row=1, column=1)
- #=======================BUTTON WIDGET==================
- btn1 = Button(TopLeft, text="Convert", width=20, bg="#006dcc", command=MeterToCenti)
- btn1.grid(row=2, columnspan=5, column=0, pady=10)
- btn2 = Button(TopRight, text="Convert", width=20, bg="#006dcc", command=CentiToMeter)
- btn2.grid(row=2, columnspan=5, column=0, pady=10)
- btn3 = Button(BottomLeft, text="Convert", width=20, bg="#006dcc", command=CelciusToFahren)
- btn3.grid(row=2, columnspan=5, column=0, pady=10)
- btn4 = Button(BottomRight, text="Convert", width=20, bg="#006dcc", command=FahrenToCelcius)
- btn4.grid(row=2, columnspan=5, column=0, pady=10)
- #=======================METHODS========================
- def MeterToCenti():
- CENTIMETER.set("")
- if METER.get() != "":
- CENTIMETER.set(float(METER.get()) * 100)
- def CentiToMeter():
- METER1.set("")
- if CENTIMETER1.get() != "":
- METER1.set(float(CENTIMETER1.get()) / 100)
- def CelciusToFahren():
- FAHRENHEIT.set("")
- if CELCIUS.get() != "":
- FAHRENHEIT.set((float(CELCIUS.get())*(9/5)) + 32)
- def FahrenToCelcius():
- CELCIUS1.set("")
- if FAHRENHEIT1.get() != "":
- CELCIUS1.set((float(FAHRENHEIT1.get()) - 32) *( 5/9))
- #=======================INITIALIZATION=================
- if __name__ == '__main__':
- root.mainloop()
Add new comment
- 69 views