# temperature conversion using tkinter

since the forum has been quiet for a while I thought to post this program , it might start new discussions . the idea is not new but it should be interesting to starters . as usual any comments is appreciated

1,447 Views

interested in python programming

``````# this program converts the Farenheit to Celsius and vice versa
# it is tested on python 3.4
# modifications are needed to get it working on python 2.xx

from tkinter import *
from tkinter import messagebox
import sys

root = Tk()
root.title('temperature converter')
text = Text(root , width = 80 , height = 30)
text.config(bg = 'blue', fg = 'yellow' , font=('Arial', 12, 'bold', 'italic'))
text.pack()

def to_write( s):

'''
this function will print to text widget
'''

text.insert(END,s)

def cel_to_far():
'''
this function asks for a number in Celsius
and converts it to Farenheit
'''
far = (clus*(9/5.0)) + 32
print('\n{} Celsius is equal to {:.1f} Farenheit '.format(clus,far) )

def far_to_cel():

'''
this function asks for a number in Farenheit
and converts it to Celsius
'''

clus = (far - 32) * (5/9.0)

print('\n{} Farenheit is equal to {:.1f} Celsius '.format(far,clus) )

def to_exit():
'''
this function stops the program
'''

quit()

sys.stdout.write = to_write

while True:
result=askinteger('options',(' Farenheit to Celsius press 1',
'\n Celsius to Farenheit press 2',
'\n Exit  press 3 '))

if result == 1:
far_to_cel()
elif result == 2:
cel_to_far()
elif result == 3:
to_exit()
elif result is None:
to_exit()
else:
messagebox.showerror('wrong input','the choice should be either 1 , or 2 or 3 ')

root.mainloop()``````
ddanbe 2,724

Nice. How about showing a table with a range like conversions between 0 and 100°C ?

I can do that , I will post it later

Great stuff thanks. I'm running 2.7

I had to change `quit()` on line 54 to:

``````    root.destroy()
sys.exit()
``````

Otherwise my run of it would hang on cancel.

Also in 2.7, I had to change some of the imports, and line 72, because several Tkinter modules have been change in 3.+ from 2.7 as follows:

``````2.7 → 3.+
Tkinter → tkinter
tkMessageBox → tkinter.messagebox
tkColorChooser → tkinter.colorchooser
tkFileDialog → tkinter.filedialog
tkCommonDialog → tkinter.commondialog
tkSimpleDialog → tkinter.simpledialog
tkFont → tkinter.font
Tkdnd → tkinter.dnd
ScrolledText → tkinter.scrolledtext
Tix → tkinter.tix
ttk → tkinter.ttk
Ref: https://stackoverflow.com/questions/673174/file-dialogs-of-tkinter-in-python-3
``````

So if the reader is not running 3.+, make the appropriate changes from the above list, and all should be good to go.

Also, to get the "Cancel" button in the dialog to work insert between lines 70 and 71 this:

``````elif result == None:
to_exit()
``````
Gribouillis 1,391

insert between lines 70 and 71 this

I updated the code accordingly. Note that a None value must be checked with the `is` operator.

thanks to @BustAcode and @Gribouillis I updated my code to work with both versions of python , also the text widget is expandable now , I tried to attach the scrollbar to text widget but no luck . here is my updated code

``````# this program converts the Farenheit to Celsius and vice versa
# it is tested on python 3.4 and python 2.7

try:
# for Python2

from Tkinter import *
import tkMessageBox
except ImportError:
# for Python3

from tkinter import *
from tkinter import messagebox

import sys

root = Tk()
root.title('temperature converter')

text = Text(root , width = 80 , height = 30 )
text.config(bg = 'blue', fg = 'yellow' , font=('Arial', 12, 'bold', 'italic'))
text.pack( fill = BOTH , expand = 1)

def to_write( s):

'''
this function will print to text widget
'''

text.insert(END,s)

def cel_to_far():
'''
this function asks for a number in Celsius
and converts it to Farenheit
'''
far = (clus*(9/5.0)) + 32
print('\n{} Celsius is equal to {:.1f} Farenheit '.format(clus,far) )

def far_to_cel():

'''
this function asks for a number in Farenheit
and converts it to Celsius
'''

clus = (far - 32) * (5/9.0)

print('\n{} Farenheit is equal to {:.1f} Celsius '.format(far,clus) )

def to_exit():
'''
this function stops the program
'''
root.destroy()
sys.exit()

sys.stdout.write = to_write

while True:
result=askinteger('options',(' Farenheit to Celsius press 1',
'\n Celsius to Farenheit press 2',
'\n Exit  press 3 '))

if result == 1:
far_to_cel()
elif result == 2:
cel_to_far()
elif result == 3:
to_exit()
elif result is None:
to_exit()
else:
messagebox.showerror('wrong input','the choice should be either 1 , or 2 or 3 ')

root.mainloop()
``````

this is what @ddanbe asked for , I hope it is not a homework assignment !!!! this can be improve but I did not have time to do it

``````# this program converts the Farenheit to Celsius and vice versa
# it also converts a series of temperatures from Celsius to Farenheit and vice versa
# it asks for the start and ending temperatures
# if we type 0(zero) as the start temperature , only the final temperature is converted
# thus the minimum starting temperature is 1 (one)
# it is tested on python 3.4 and python 2.7

try:
# for Python2

from Tkinter import *
import tkMessageBox
except ImportError:

# for Python3

from tkinter import *
from tkinter import messagebox

import sys

root = Tk()
root.title('temperature converter')

text = Text(root , width = 80 , height = 30 )
text.config(bg = 'blue', fg = 'yellow' , font=('Arial', 12, 'bold', 'italic'))
text.pack( fill = BOTH , expand = 1)

def to_write( s):

'''
this function will print to text widget
'''

text.insert(END,s)

def cel_to_far():
'''
this function converts a range of temperatures
from Celsius to Farenheit with increments of
5 degree and prints them on the text widget

'''
for i in range (start,end + 1 , 5 ):

farenheit = (i * 9/5.0) + 32
print('{} celsius is equal to {:.1f} farenheit ' . format(i,farenheit))

def far_to_cel():

'''
this function converts a range of temperatures
from Farenheit to Celsius with increments of
5 degree and prints them on the text widget
'''
for i in range(start,end + 1 , 5 ):
clus = (i - 32) * (5/9.0)

print('{} Farenheit is equal to {:.1f} Celsius '.format(i , clus) )

def to_exit():
'''
this function stops the program
'''
root.destroy()
sys.exit()

sys.stdout.write = to_write

while True:

'\n 2:  Celsius to Farenheit ',

'\n 3:  Exit  '))

if result == 1:

'\ntype 0 (zero) if you do not want a range \n'))

if start == 0:
clus = (end - 32) * (5/9.0)
print('\n{} Farenheit is equal to {:.1f} Celsius '.format(end , clus) )
elif start and end:
far_to_cel()
elif not start and not end:
to_exit()

elif result == 2:

'\ntype 0 (zero) if you do not want a range\n'))

if start == 0:
farenheit = (end * 9/5.0) + 32
print('{0} celsius is equal to {1} farenheit ' . format(end,farenheit))
elif start and end:
cel_to_far()
elif not start and not end:
to_exit()

elif result == 3:
to_exit()
elif result is None:
to_exit()
else:
messagebox.showerror('wrong input','the choice should be either 1 , or 2 or 3 ')

root.mainloop()
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.