Programming Exercise 8.3
Instructions
breezypythongui.py
temperatureconverter.py
Write a GUI-based program that allows the user to convert temperature values between degrees Fahrenheit and degrees Celsius. The interface should have labeled entry fields for these two values.
File: temperatureconverter.py
Project 8.3
Temperature conversion between Fahrenheit and Celsius.
Illustrates the use of numeric data fields.
These components should be arranged in a grid where the labels occupy the first row and the corresponding fields occupy the second row.
from breezypythongui import EasyFrame
At start-up, the Fahrenheit field should contain 32.0, and the Celsius field should contain 0.0.
class TemperatureConverter(EasyFrame):
"""Temperature conversion program."""
def __init__(self):
"""Sets up the window and widgets."""
EasyFrame.__init__(self, title="Temperature Converter")
self.addLabel(text="Celsius", row=0, column=0)
self.celsiusField = self.addFloatField(value=0.0, row=1, column=0)
self.addLabel(text="Fahrenheit", row=0, column=1)
self.fahrField = self.addFloatField(value=32.0, row=1, column=1)
self.addButton(text=">>>>", row=2, column=0, command=self.computeFahr)
self.addButton(text="<<<<", row=2, column=1, command=self.computeCelsius)
# The controller methods
def computeFahr(self):
"""Inputs the Celsius degrees and outputs the Fahrenheit degrees."""
celsius = self.celsiusField.getNumber()
fahrenheit = (9/5) * celsius + 32
self.fahrField.setNumber(fahrenheit)
def computeCelsius(self):
"""Inputs the Fahrenheit degrees and outputs the Celsius degrees."""
fahrenheit = self.fahrField.getNumber()
celsius = (fahrenheit - 32) * (5/9)
self.celsiusField.setNumber(celsius)
def main():
"""Instantiate and pop up the window."""
TemperatureConverter().mainloop()
if __name__ == "__main__":
try:
while True:
main()
except KeyboardInterrupt:
print("Program closed.")
Grading
When you have completed your program, click the Submit button to record your score.