Enhance the temperature conversion program of Exercise 10.4 by adding the Kelvin temper- ature scale. The program should also allow the user to make conversions between any two scales. Use the following formula for the conversion between Kelvin and Celsius (in addition to the formula in Exercise 10.4): Kelvin = Celsius + 273

What will be an ideal response?


```
# GUI that offers temperature conversion

from Tkinter import *
from tkMessageBox import *

class TemperatureConversion( Frame ):
"""Temperature conversion of user input"""

def __init__( self ):
"""Create an Entry and a button component"""
Frame.__init__( self )
self.grid( sticky = W+E+N+S )
self.master.title( "Temperature Conversion" )
self.master.rowconfigure( 0, weight = 1 )
self.master.columnconfigure( 0, weight = 1 )

# list of conversion selections
temperatureConversions = [ "Celsius to Fahrenheit",
"Celsius to Kelvin", "Fahrenheit to Kelvin",
"Fahrenheit to Celsius", "Kelvin to Celsius",
"Kelvin to Fahrenheit" ]
self.chosenConversion = StringVar()
gridRow = 0

# create the radio buttons
for conversion in temperatureConversions:
self.aButton = Radiobutton( self, text = conversion,
variable = self.chosenConversion, value = conversion,
command = self.convertTemperature )
self.aButton.grid( row = gridRow, column = 0,
sticky = W+E )
gridRow += 1

# create an Entry widget for the temperature
self.temperature = Entry( self, width = 30,
name = "temperature" )
self.temperature.grid( row = 7, sticky = E+W )

def convertTemperature( self ):
"""Call appropriate conversion function"""

# get user input from Entry widget
input = self.temperature.get()

# user did not enter a temperature
if not input:
return

temperature = float( input )

# chosen conversion
conversion = self.chosenConversion.get()

# call appropriate conversion function
if conversion == "Celsius to Fahrenheit":
t = self.CelsiusToFahrenheit( temperature )
elif conversion == "Celsius to Kelvin":
t = self.CelsiusToKelvin( temperature )
elif conversion == "Fahrenheit to Celsius":
t = self.FahrenheitToCelsius( temperature )
elif conversion == "Fahrenheit to Kelvin":
t = self.FahrenheitToKelvin( temperature )
elif conversion == "Kelvin to Celsius":
t = self.KelvinToCelsius( temperature )
elif conversion == "Kelvin to Fahrenheit":
t = self.KelvinToFahrenheit( temperature )

# call the display function
self.display( t )

def display( self, information ):
"""Display converted temperature"""

showinfo( "Conversion", information )

def FahrenheitToCelsius( self, temperature ):
"""Convert Fahrenheit temperature to Celsius"""

return ( ( 5.0 / 9 ) * ( temperature - 32 ) )

def FahrenheitToKelvin( self, temperature ):
"""Convert Fahrenheit temperature to Kelvin"""

celsius = self.FahrenheitToCelsius( temperature )
return self.CelsiusToKelvin( celsius )

def CelsiusToFahrenheit( self, temperature ):
"""Convert Celsius temperature to Fahrenheit"""

return ( ( 9.0 / 5 ) * temperature + 32 )

def CelsiusToKelvin( self, temperature ):
"""Convert Celsius temperature to Kelvin"""

return ( temperature + 273 )

def KelvinToCelsius( self, temperature ):
"""Convert Kelvin temperature to Celsius"""

return ( temperature - 273 )

def KelvinToFahrenheit( self, temperature ):
"""Convert Kelvin temperature to Fahrenheit"""

celsius = self.KelvinToCelsius( temperature )
return self.CelsiusToFahrenheit( celsius )

def main():
TemperatureConversion().mainloop()

if __name__ == "__main__":
main()
```
![15063|306x164](upload://5npzYjQ2FVDOFU7XBum3DufX9VS.png)

Computer Science & Information Technology

You might also like to view...

In a transaction processing cycle, ______ involves updating one or more databases in an organization with new transactions.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology

Word can automatically generate a table of contents from built-in ________ styles

A) heading B) character C) linked D) paragraph

Computer Science & Information Technology

Blank design templates can be downloaded and must be saved as templates

Indicate whether the statement is true or false

Computer Science & Information Technology

When you use tabs in a text frame that has a text inset, the tab ruler aligns itself on the left edge of where the text is inset, not the left edge of the text frame.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology