-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20171627_서명희_assignment10.py
113 lines (78 loc) · 3.32 KB
/
20171627_서명희_assignment10.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
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtWidgets import QLineEdit, QToolButton
from PyQt5.QtWidgets import QSizePolicy
from PyQt5.QtWidgets import QLayout, QGridLayout
class Button(QToolButton): #button에 setText를 해주는 메서드 클래스
def __init__(self, text, cb):
super().__init__()
self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
self.setText(text)
self.clicked.connect(cb)
def sizeHint(self):
size = super(Button, self).sizeHint()
size.setHeight(size.height() + 20)
size.setWidth(max(size.width(), size.height()))
return size
class Calculator(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
# Display Window
self.display = QLineEdit()
self.display.setReadOnly(True)
self.display.setAlignment(Qt.AlignRight)
self.display.setMaxLength(15)
# Digit Buttons
self.digitButton = [x for x in range(0, 10)]
for i in range(10): #for 문으로 수정
self.digitButton[i] = Button(str(i), self.buttonClicked)
# . and = Buttons
self.decButton = Button('.', self.buttonClicked)
self.eqButton = Button('=', self.buttonClicked)
# Operator Buttons
self.mulButton = Button('*', self.buttonClicked)
self.divButton = Button('/', self.buttonClicked)
self.addButton = Button('+', self.buttonClicked)
self.subButton = Button('-', self.buttonClicked)
# Parentheses Buttons
self.lparButton = Button('(', self.buttonClicked)
self.rparButton = Button(')', self.buttonClicked)
# Clear Button
self.clearButton = Button('C', self.buttonClicked)
# Layout
mainLayout = QGridLayout()
mainLayout.setSizeConstraint(QLayout.SetFixedSize)
mainLayout.addWidget(self.display, 0, 0, 1, 2)
numLayout = QGridLayout()
for i in range(10): #for문으로 수정
numLayout.addWidget(self.digitButton[i], (9-i)//3, 0 if i==0 else (i-1)%3)
numLayout.addWidget(self.decButton, 3, 1)
numLayout.addWidget(self.eqButton, 3, 2)
mainLayout.addLayout(numLayout, 1, 0)
opLayout = QGridLayout()
opLayout.addWidget(self.mulButton, 0, 0)
opLayout.addWidget(self.divButton, 0, 1)
opLayout.addWidget(self.addButton, 1, 0)
opLayout.addWidget(self.subButton, 1, 1)
opLayout.addWidget(self.lparButton, 2, 0)
opLayout.addWidget(self.rparButton, 2, 1)
opLayout.addWidget(self.clearButton, 3, 0)
mainLayout.addLayout(opLayout, 1, 1)
self.setLayout(mainLayout)
self.setWindowTitle("My Calculator")
def buttonClicked(self):
button = self.sender()
key = button.text()
if key == '=':
result = str(eval(self.display.text()))
self.display.setText(result)
elif key == 'C':
self.display.setText('')
else:
self.display.setText(self.display.text() + key)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
calc = Calculator()
calc.show()
sys.exit(app.exec_())