Advertisement
TorroesPrime

Untitled

Apr 12th, 2022
1,106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.19 KB | None | 0 0
  1. """
  2. File: temperatureconverter.py
  3. Project 8.4
  4. Temperature conversion between Fahrenheit and Celsius.
  5. Illustrates the use of numeric data fields.
  6. Responds to a return key event in the entry fields.
  7. """
  8.  
  9. from breezypythongui import EasyFrame
  10.  
  11. class TemperatureConverter(EasyFrame):
  12.     """A termperature conversion program."""
  13.  
  14.     def __init__(self):
  15.         """Sets up the window and widgets."""
  16.         EasyFrame.__init__(self, title = "Temperature Converter")
  17.  
  18.         # Label and field for Celsius
  19.         self.addLabel(text = "Celsius",
  20.                       row = 0, column = 0)
  21.         self.celsiusField = self.addFloatField(value = 0.0,
  22.                                                row = 1,
  23.                                                column = 0,
  24.                                                precision = 2)
  25.         # Label and field for Fahrenheit
  26.         self.addLabel(text = "Fahrenheit",
  27.                       row = 0, column = 1)
  28.         self.fahrField = self.addFloatField(value = 32.0,
  29.                                             row = 1,
  30.                                             column = 1,
  31.                                             precision = 2)
  32.         # Celsius to Fahrenheit button
  33.         self.addButton(text = ">>>>",
  34.                        row = 2, column = 0,
  35.                        command = self.computeFahr)
  36.  
  37.         # Fahrenheit to Celsius button
  38.         self.addButton(text = "<<<<",
  39.                        row = 2, column = 1,
  40.                        command = self.computeCelsius)
  41.  
  42.     # The controller methods
  43.     def computeFahr(self):
  44.         """Inputs the Celsius degrees
  45.        and outputs the Fahrenheit degrees."""
  46.         degrees = self.celsiusField.getNumber()
  47.         degrees = degrees * 9 / 5 + 32
  48.         self.fahrField.setNumber(degrees)
  49.    
  50.     def computeCelsius(self):
  51.         """Inputs the Fahrenheit degrees
  52.        and outputs the Celsius degrees."""
  53.         degrees = self.fahrField.getNumber()
  54.         degrees = (degrees - 32) * 5 / 9
  55.         self.celsiusField.setNumber(degrees)
  56.  
  57. def main():
  58.     """Instantiate and pop up the window."""
  59.     TemperatureConverter().mainloop()
  60.  
  61. if __name__ == "__main__":
  62.     main()
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement