계산기 앱

sz L·2023년 3월 20일
0

MINII_Project

목록 보기
15/15
post-thumbnail
  • QT Designer
import sys
from PyQt5 import uic
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

class qtApp(QWidget):
    def __init__(self):
        super().__init__()
        uic.loadUi('./studyPyQt/calculator.ui',self)
        self.setWindowIcon(QIcon('./studyPyQt/cal.png'))

        # 시그널 16개에 슬롯함수는 1개
        self.btn_C.clicked.connect(self.btnClicked)
        self.btn_number0.clicked.connect(self.btnClicked)
        self.btn_number1.clicked.connect(self.btnClicked)
        self.btn_number2.clicked.connect(self.btnClicked)
        self.btn_number3.clicked.connect(self.btnClicked)
        self.btn_number4.clicked.connect(self.btnClicked)
        self.btn_number5.clicked.connect(self.btnClicked)
        self.btn_number6.clicked.connect(self.btnClicked)
        self.btn_number7.clicked.connect(self.btnClicked)
        self.btn_number8.clicked.connect(self.btnClicked)
        self.btn_number9.clicked.connect(self.btnClicked)
        self.btn_result.clicked.connect(self.btnClicked)
        self.btn_minus.clicked.connect(self.btnClicked)
        self.btn_add.clicked.connect(self.btnClicked)
        self.btn_multipy.clicked.connect(self.btnClicked)
        self.btn_divide.clicked.connect(self.btnClicked)

        self.txt_view.setEnabled(False)
        self.text_value = ''

    def btnClicked(self):
        btn_val = self.sender().text()
        if btn_val == 'C': # clear
            print('clear')
            self.txt_view.setText('0')
            self.text_value = ''
        elif btn_val == '=': # 계산결과
            print('=')
            try:
                result = eval(self.text_value.lstrip('0'))
                print(round(result,4)) # 예를들어 10/6 할 때 소수점 자리 잘라주기 
                self.txt_view.setText(str(round(result,4)))
            except: 
                self.txt_view.setText('ERROR')
        else:
            if btn_val == 'X':
                btn_val = '*'
            self.text_value += btn_val
            print(self.text_value)
            self.txt_view.setText(self.text_value)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = qtApp() # MyApp(9일차 참고)
    ex.show()
    sys.exit(app.exec_())      
  • 실행화면
profile
가랑비는 맞는다 하지만 폭풍은 내 것이야

0개의 댓글