일반적인 기능 완료
추가기능
1. MR,MS,MC, 적용
2. BS(back space) 기능 추가
3. 숫자 display text 크기 증가(알아보기 쉽게)
다음 예정
1. 주석 추가(이해, 분석등 내용 추가)
import sys
from PySide6 import QtCore, QtGui
from PySide6.QtWidgets import QApplication, QMainWindow, QLineEdit, QPushButton, QHBoxLayout, QVBoxLayout, QWidget, QGridLayout, QLabel
from PySide6.QtCore import Slot, Qt
import re
result = ''
result_state = False
count = 0
MS_number = 0
class MyWidget(QMainWindow):
def __init__(self):
super().__init__()
self.setFixedSize(300, 600)
self.setWindowTitle("Caculate by KKD")
print(id(self))
# create display
self.text_edit = QLineEdit("", self)
self.text_edit2 = QLineEdit("", self)
self.text_edit.setFixedSize(280, 40)
self.text_edit2.setFixedSize(100, 20)
self.text_label = QLabel("Memory Number")
font = self.text_label.font()
font.setPointSize(10)
self.text_label.setFont(font)
font = self.text_edit.font()
font.setPointSize(15)
self.text_edit.setFont(font)
# 오른쪽 정렬
self.text_edit.setAlignment(Qt.AlignRight)
self.text_edit2.setAlignment(Qt.AlignRight)
# 읽기만 가능하게
self.text_edit.setReadOnly(True)
self.text_edit2.setReadOnly(True)
# button 만들기
self.createButtons()
# set main layout
main_layout = QVBoxLayout()
main_layout2 = QHBoxLayout()
main_layout.addWidget(self.text_edit)
main_layout2.addWidget(self.text_label)
main_layout2.addWidget(self.text_edit2)
main_layout.addLayout(main_layout2)
main_layout.addLayout(self.buttons_layout)
central_widget = QWidget(self)
central_widget.setLayout(main_layout)
self.setCentralWidget(central_widget)
def createButtons(self):
global count
self.buttons = {
"MC": (0, 0),
"MR": (0, 1),
"MS": (0, 2),
"BS": (0, 3),
"(": (0, 4),
"(": (1, 0),
")": (1, 1),
"%": (1, 2),
"AC": (1, 3),
"7": (2, 0),
"8": (2, 1),
"9": (2, 2),
"/": (2, 3),
"4": (3, 0),
"5": (3, 1),
"6": (3, 2),
"*": (3, 3),
"1": (4, 0),
"2": (4, 1),
"3": (4, 2),
"-": (4, 3),
"0": (5, 0),
".": (5, 1),
"=": (5, 2),
"+": (5, 3)
}
# print(self.buttons.items())
# print(type(self.buttons.items()))
self.buttons_layout = QGridLayout()
for btn_text, pos in self.buttons.items():
# print("btn_text = ",btn_text)
# print("pos = ",pos)
button = QPushButton(btn_text)
# print(type(button))
button.clicked.connect(self.handle_button)
self.buttons_layout.addWidget(button, pos[0], pos[1])
button.setFixedSize(50, 50)
font = button.font()
font.setPointSize(15)
button.setFont(font)
def handle_button(self):
global result_state
global MS_number
# print(result_state)
button = self.sender()
if button.text() == "=":
try:
temp = self.text_edit.text()
result = eval(self.text_edit.text())
self.text_edit.setText(temp + button.text() + str(result))
result_state = True
except:
self.text_edit.setText("Error")
elif button.text() == "AC":
self.text_edit.clear()
elif button.text() == "BS":
text = self.text_edit.text()
print(text[:-1])
print(len(text))
if len(text) > 0:
self.text_edit.setText(text[:-1])
elif button.text() == "MS":
result_state = False
MS_number = self.text_edit.text()
value = MS_number[MS_number.index("=")+1:]
self.text_edit.clear()
self.text_edit2.setText(value)
MS_number = value
print(MS_number)
elif button.text() == "MR":
print(MS_number)
result_state = False
self.text_edit.setText(self.text_edit.text() + MS_number)
elif button.text() == "MC":
MS_number = ''
print(MS_number)
# self.text_edit.setText(MS_number)
self.text_edit2.setText(MS_number)
else:
if result_state == True:
self.text_edit.clear()
self.text_edit.setText(self.text_edit.text() + button.text())
result_state = False
else:
self.text_edit.setText(self.text_edit.text() + button.text())
if __name__ == "__main__":
app = QApplication()
widget = MyWidget()
widget.show()
sys.exit(app.exec())
댓글