Calculator.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import sys
  2. from PyQt5.uic import *
  3. from PyQt5.QtCore import *
  4. from PyQt5.QtGui import *
  5. from PyQt5.QtWidgets import *
  6. class Calculator(QMainWindow):
  7. def __init__(self,title='Calculator',parent=None,uiFile=None):
  8. super(Calculator,self).__init__(parent)
  9. self.window=QMainWindow()
  10. self.window.setWindowTitle('Calculator App : Made by Rahul')
  11. loadUi(uiFile,self.window)
  12. self.buttons_num=list()
  13. for i in range(10):
  14. butt_name='button_'+str(i)
  15. button=self.window.findChild(QPushButton,butt_name)
  16. button.released.connect(self.on_buttons_num_released)
  17. self.buttons_num.append(button)
  18. self.button_dot=self.window.findChild(QPushButton,'button_dot')
  19. self.button_dot.released.connect(self.on_button_dot_released)
  20. self.button_ac=self.window.findChild(QPushButton,'button_ac')
  21. self.button_ac.released.connect(self.on_button_ac_released)
  22. self.button_ac=self.window.findChild(QPushButton,'button_del')
  23. self.button_ac.released.connect(self.on_button_del_released)
  24. self.button_ac=self.window.findChild(QPushButton,'button_ans')
  25. self.button_ac.released.connect(self.on_button_ans_released)
  26. self.button_ac=self.window.findChild(QPushButton,'button_equal')
  27. self.button_ac.released.connect(self.on_button_equal_released)
  28. self.button_ac=self.window.findChild(QPushButton,'button_div')
  29. self.button_ac.released.connect(self.on_buttons_opr_released)
  30. self.button_ac=self.window.findChild(QPushButton,'button_mult')
  31. self.button_ac.released.connect(self.on_buttons_opr_released)
  32. self.button_ac=self.window.findChild(QPushButton,'button_add')
  33. self.button_ac.released.connect(self.on_buttons_opr_released)
  34. self.button_ac=self.window.findChild(QPushButton,'button_sub')
  35. self.button_ac.released.connect(self.on_buttons_opr_released)
  36. self.button_ac=self.window.findChild(QPushButton,'button_exp')
  37. self.button_ac.released.connect(self.on_buttons_opr_released)
  38. self.display=self.window.findChild(QLabel,'display')
  39. self.ans_label=self.window.findChild(QLabel,'ans_label')
  40. self.answer=0.0
  41. self.active_operator=None
  42. self.window.show()
  43. @pyqtSlot()
  44. def on_button_ac_released(self):
  45. self.display.setText('0')
  46. @pyqtSlot()
  47. def on_buttons_num_released(self):
  48. button=self.sender()
  49. if self.display.text()=='0' or self.display.text()=='0.0':
  50. self.display.setText(button.text())
  51. else:
  52. text=self.display.text()
  53. self.display.setText(text+button.text())
  54. @pyqtSlot()
  55. def on_button_dot_released(self):
  56. text=self.display.text()
  57. if '.' not in text and text!='':
  58. self.display.setText(text+'.')
  59. @pyqtSlot()
  60. def on_button_del_released(self):
  61. text=self.display.text()
  62. if text[0:-1]=='':
  63. self.display.setText('0')
  64. else:
  65. self.display.setText(text[0:-1])
  66. @pyqtSlot()
  67. def on_buttons_opr_released(self):
  68. button=self.sender()
  69. operator=button.text()
  70. value=float(self.display.text())
  71. if self.active_operator==None:
  72. self.answer=value
  73. else:
  74. self.answer=self.operate(value,self.active_operator)
  75. self.active_operator=operator
  76. self.ans_label.setText('= '+str(self.answer)+ ' '+self.active_operator)
  77. self.display.setText('0')
  78. @pyqtSlot()
  79. def on_button_ans_released(self):
  80. self.display.setText(str(self.answer))
  81. @pyqtSlot()
  82. def on_button_equal_released(self):
  83. value=float(self.display.text())
  84. if self.active_operator==None:
  85. self.answer=value
  86. else:
  87. self.answer=self.operate(value,self.active_operator)
  88. self.active_operator=None
  89. self.ans_label.setText('= '+str(self.answer))
  90. self.display.setText(str(self.answer))
  91. def operate(self,value,operator):
  92. if operator=='+':
  93. self.answer+=value
  94. elif operator=='-':
  95. self.answer-=value
  96. elif operator=='*':
  97. self.answer*=value
  98. elif operator=='/':
  99. self.answer/=value
  100. elif operator=='^':
  101. self.answer=self.answer**value
  102. return self.answer
  103. if __name__=='__main__':
  104. testApp=QApplication(sys.argv)
  105. calc=Calculator(title='Calculator',parent=None,uiFile='form/ui_calculator.ui')
  106. testApp.exec_()