-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator_main.py
122 lines (100 loc) · 5.09 KB
/
calculator_main.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
import sys
from PyQt5.QtWidgets import *
class Main(QDialog):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
main_layout = QVBoxLayout()
### 각 위젯을 배치할 레이아웃을 미리 만들어 둠
layout_operation = QHBoxLayout()
layout_clear_equal = QHBoxLayout()
layout_number = QGridLayout()
layout_equation_solution = QFormLayout()
### 수식 입력과 답 출력을 위한 LineEdit 위젯 생성
label_equation = QLabel("Equation: ")
label_solution = QLabel("Number: ")
self.equation = QLineEdit("")
self.solution = QLineEdit("")
### layout_equation_solution 레이아웃에 수식, 답 위젯을 추가
layout_equation_solution.addRow(label_equation, self.equation)
layout_equation_solution.addRow(label_solution, self.solution)
### 사칙연상 버튼 생성
button_plus = QPushButton("+")
button_minus = QPushButton("-")
button_product = QPushButton("x")
button_division = QPushButton("/")
### 사칙연산 버튼을 클릭했을 때, 각 사칙연산 부호가 수식창에 추가될 수 있도록 시그널 설정
button_plus.clicked.connect(lambda state, operation = "+": self.button_operation_clicked(operation))
button_minus.clicked.connect(lambda state, operation = "-": self.button_operation_clicked(operation))
button_product.clicked.connect(lambda state, operation = "*": self.button_operation_clicked(operation))
button_division.clicked.connect(lambda state, operation = "/": self.button_operation_clicked(operation))
### 사칙연산 버튼을 layout_operation 레이아웃에 추가
layout_operation.addWidget(button_plus)
layout_operation.addWidget(button_minus)
layout_operation.addWidget(button_product)
layout_operation.addWidget(button_division)
### =, clear, backspace 버튼 생성
button_equal = QPushButton("=")
button_clear = QPushButton("Clear")
button_backspace = QPushButton("Backspace")
### =, clear, backspace 버튼 클릭 시 시그널 설정
button_equal.clicked.connect(self.button_equal_clicked)
button_clear.clicked.connect(self.button_clear_clicked)
button_backspace.clicked.connect(self.button_backspace_clicked)
### =, clear, backspace 버튼을 layout_clear_equal 레이아웃에 추가
layout_clear_equal.addWidget(button_clear)
layout_clear_equal.addWidget(button_backspace)
layout_clear_equal.addWidget(button_equal)
### 숫자 버튼 생성하고, layout_number 레이아웃에 추가
### 각 숫자 버튼을 클릭했을 때, 숫자가 수식창에 입력 될 수 있도록 시그널 설정
number_button_dict = {}
for number in range(0, 10):
number_button_dict[number] = QPushButton(str(number))
number_button_dict[number].clicked.connect(lambda state, num = number:
self.number_button_clicked(num))
if number >0:
x,y = divmod(number-1, 3)
layout_number.addWidget(number_button_dict[number], x, y)
elif number==0:
layout_number.addWidget(number_button_dict[number], 3, 1)
### 소숫점 버튼과 00 버튼을 입력하고 시그널 설정
button_dot = QPushButton(".")
button_dot.clicked.connect(lambda state, num = ".": self.number_button_clicked(num))
layout_number.addWidget(button_dot, 3, 2)
button_double_zero = QPushButton("00")
button_double_zero.clicked.connect(lambda state, num = "00": self.number_button_clicked(num))
layout_number.addWidget(button_double_zero, 3, 0)
### 각 레이아웃을 main_layout 레이아웃에 추가
main_layout.addLayout(layout_equation_solution)
main_layout.addLayout(layout_operation)
main_layout.addLayout(layout_clear_equal)
main_layout.addLayout(layout_number)
self.setLayout(main_layout)
self.show()
#################
### functions ###
#################
def number_button_clicked(self, num):
equation = self.equation.text()
equation += str(num)
self.equation.setText(equation)
def button_operation_clicked(self, operation):
equation = self.equation.text()
equation += operation
self.equation.setText(equation)
def button_equal_clicked(self):
equation = self.equation.text()
solution = eval(equation)
self.solution.setText(str(solution))
def button_clear_clicked(self):
self.equation.setText("")
self.solution.setText("")
def button_backspace_clicked(self):
equation = self.equation.text()
equation = equation[:-1]
self.equation.setText(equation)
if __name__ == '__main__':
app = QApplication(sys.argv)
main = Main()
sys.exit(app.exec_())