-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCalculator.py
141 lines (106 loc) · 4.49 KB
/
Calculator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import sys
from PyQt5.uic import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Calculator(QMainWindow):
def __init__(self,title='Calculator',parent=None,uiFile=None):
super(Calculator,self).__init__(parent)
self.window=QMainWindow()
self.window.setWindowTitle(title)
loadUi(uiFile,self.window)
self.buttons_num=list()
for i in range(10):
butt_name='button_'+str(i)
button=self.window.findChild(QPushButton,butt_name)
button.released.connect(self.on_buttons_num_released)
self.buttons_num.append(button)
self.button_dot=self.window.findChild(QPushButton,'button_dot')
self.button_dot.released.connect(self.on_button_dot_released)
self.button_ac=self.window.findChild(QPushButton,'button_ac')
self.button_ac.released.connect(self.on_button_ac_released)
self.button_ac=self.window.findChild(QPushButton,'button_del')
self.button_ac.released.connect(self.on_button_del_released)
self.button_ac=self.window.findChild(QPushButton,'button_ans')
self.button_ac.released.connect(self.on_button_ans_released)
self.button_ac=self.window.findChild(QPushButton,'button_equal')
self.button_ac.released.connect(self.on_button_equal_released)
self.button_ac=self.window.findChild(QPushButton,'button_div')
self.button_ac.released.connect(self.on_buttons_opr_released)
self.button_ac=self.window.findChild(QPushButton,'button_mult')
self.button_ac.released.connect(self.on_buttons_opr_released)
self.button_ac=self.window.findChild(QPushButton,'button_add')
self.button_ac.released.connect(self.on_buttons_opr_released)
self.button_ac=self.window.findChild(QPushButton,'button_sub')
self.button_ac.released.connect(self.on_buttons_opr_released)
self.button_ac=self.window.findChild(QPushButton,'button_exp')
self.button_ac.released.connect(self.on_buttons_opr_released)
self.display=self.window.findChild(QLabel,'display')
self.ans_label=self.window.findChild(QLabel,'ans_label')
self.answer=0.0
self.active_operator=None
self.window.show()
@pyqtSlot()
def on_button_ac_released(self):
self.display.setText('0')
@pyqtSlot()
def on_buttons_num_released(self):
button=self.sender()
if self.display.text()=='0' or self.display.text()=='0.0':
self.display.setText(button.text())
else:
text=self.display.text()
self.display.setText(text+button.text())
@pyqtSlot()
def on_button_dot_released(self):
text=self.display.text()
if '.' not in text and text!='':
self.display.setText(text+'.')
@pyqtSlot()
def on_button_del_released(self):
text=self.display.text()
if text[0:-1]=='':
self.display.setText('0')
else:
self.display.setText(text[0:-1])
@pyqtSlot()
def on_buttons_opr_released(self):
button=self.sender()
operator=button.text()
value=float(self.display.text())
if self.active_operator==None:
self.answer=value
else:
self.answer=self.operate(value,self.active_operator)
self.active_operator=operator
self.ans_label.setText('= '+str(self.answer)+ ' '+self.active_operator)
self.display.setText('0')
@pyqtSlot()
def on_button_ans_released(self):
self.display.setText(str(self.answer))
@pyqtSlot()
def on_button_equal_released(self):
value=float(self.display.text())
if self.active_operator==None:
self.answer=value
else:
self.answer=self.operate(value,self.active_operator)
self.active_operator=None
self.ans_label.setText('= '+str(self.answer))
self.display.setText(str(self.answer))
def operate(self,value,operator):
if operator=='+':
self.answer+=value
elif operator=='-':
self.answer-=value
elif operator=='*':
self.answer*=value
elif operator=='/':
self.answer/=value
elif operator=='^':
self.answer=self.answer**value
return self.answer
if __name__=='__main__':
testApp=QApplication(sys.argv)
calc=Calculator(title='Calculator App : Made by Rahul',parent=None,uiFile='form/ui_calculator.ui')
testApp.exec_()