Guest User

Untitled

a guest
Jan 18th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. import math
  2. import sys
  3. from PyQt5.QtWidgets import *
  4.  
  5.  
  6. # Logic of the Program
  7. class Button:
  8. def __init__(self, text, results):
  9. self.b = QPushButton(str(text))
  10. self.text = text
  11. self.results = results
  12. self.b.clicked.connect(lambda: self.handleInput(self.text))
  13.  
  14. # How the program handles having its buttons pushed
  15. def handleInput(self, v):
  16.  
  17. # If the equals sign is pressed, evaluatate the results of the QLineEdit
  18. if v == "=":
  19. res = eval(self.results.text())
  20. self.results.setText(str(res))
  21.  
  22. # If the AC button is pressed, set the results to an empty string (essentially clearing the equation)
  23. elif v == "AC":
  24. self.results.setText("")
  25.  
  26. # If square root is pressed use function in the math module
  27. elif v == "√":
  28. value = float(self.results.text())
  29. self.results.setText(str(math.sqrt(value)))
  30.  
  31. # If DEL is selected, subtract one from the line edit
  32. elif v == "DEL":
  33. current_value = self.results.text()
  34. self.results.setText(current_value[:-1])
  35.  
  36. # If a number button is pressed, set the text to the value of that number
  37. else:
  38. current_value = self.results.text()
  39. new_value = current_value + str(v)
  40. self.results.setText(new_value)
  41.  
  42.  
  43. # Design of the Program
  44. class Application(QWidget):
  45. def __init__(self):
  46. super().__init__()
  47. self.setWindowTitle("Peter's Homemade Calculator")
  48. self.CreateApp()
  49.  
  50. # Designing the layout of the app
  51. def CreateApp(self):
  52.  
  53. # Create our grid
  54. grid = QGridLayout()
  55.  
  56. # Also allows the text box for manual input
  57. results = QLineEdit()
  58.  
  59. # List of buttons before initializing them
  60. buttons = ["AC", "√", "DEL", "/",
  61. 7, 8, 9, "*",
  62. 4, 5, 6, "-",
  63. 1, 2, 3, "+",
  64. 0, ".", "="]
  65.  
  66. # Adding the dimensions of each button to the widget. In column and row 1 [0],
  67. # the button row is 1 column tall and 4 wide
  68. grid.addWidget(results, 0, 0, 1, 4)
  69.  
  70. row = 1
  71. column = 0
  72.  
  73. # Creating the loop to generate buttons.
  74. for button in buttons:
  75. # If column after 3, return to start new row
  76. if column > 3:
  77. column = 0
  78. row += 1
  79.  
  80. # Instantiating the button object. Giving the properties button and results
  81. button_object = Button(button, results)
  82.  
  83. if button == 0:
  84. grid.addWidget(button_object.b, row, column, 1, 2)
  85. column += 1
  86. else:
  87. grid.addWidget(button_object.b, row, column, 1, 1)
  88.  
  89. column += 1
  90.  
  91. # Tells the program to set the layout to our grid object
  92. self.setLayout(grid)
  93.  
  94. # Shows the application
  95. self.show()
  96.  
  97.  
  98. # Runs the program
  99. if __name__ == "__main__":
  100. app = QApplication(sys.argv)
  101. window = Application()
  102. sys.exit(app.exec())
Add Comment
Please, Sign In to add comment