示例#1
0
    def buttonClicked(self):
        except_list = ['Error!', '0']
        if self.display.text() in except_list:
            self.display.setText('')

        button = self.sender()
        key = button.text()

        key_dic = {}

        if key == '=':
            try:
                result = str(eval(self.display.text()))
            except:
                result = 'Error!'
            self.display.setText(result)
        elif key == 'C':
            self.display.clear()
        elif key in constantDic.keys():
            self.display.setText(self.display.text() + constantDic[key])
        elif key in functionDic.keys():
            n = eval(self.display.text())
            value = eval(functionDic[key])
            self.display.setText(str(value))
        else:
            self.display.setText(self.display.text() + key)
    def __init__(self, parent=None):
        super().__init__(parent)

        # Display Window
        self.display = QLineEdit('0')
        self.display.setReadOnly(True)
        self.display.setAlignment(Qt.AlignRight)
        self.display.setMaxLength(15)

        # Button Creation and Placement
        numLayout = QGridLayout()
        opLayout = QGridLayout()
        constLayout = QGridLayout()
        funcLayout = QGridLayout()

        buttonGroups = {
            'num': {
                'buttons': numPadList,
                'layout': numLayout
            },
            'op': {
                'buttons': operatorList,
                'layout': opLayout
            },
            'constants': {
                'buttons': [[x] for x in constantDic.keys()],
                'layout': constLayout
            },
            'functions': {
                'buttons': [[x] for x in functionDic.keys()],
                'layout': funcLayout
            },
        }

        for label in buttonGroups.keys():
            buttonPad = buttonGroups[label]
            for row, btn_list in enumerate(buttonPad['buttons']):
                for col, btnText in enumerate(btn_list):
                    button = Button(btnText, self.buttonClicked)
                    buttonPad['layout'].addWidget(button, row, col)

        # Layout
        mainLayout = QGridLayout()
        mainLayout.setSizeConstraint(QLayout.SetFixedSize)

        mainLayout.addWidget(self.display, 0, 0, 1, 2)
        mainLayout.addLayout(numLayout, 1, 0)
        mainLayout.addLayout(opLayout, 1, 1)
        mainLayout.addLayout(constLayout, 2, 0)
        mainLayout.addLayout(funcLayout, 2, 1)

        self.setLayout(mainLayout)

        self.setWindowTitle("My Calculator")
示例#3
0
    def buttonClicked(self):

        if self.display.text() == 'Error!':
            self.display.setText('')

        button = self.sender()
        key = button.text()

        if key == '=':
            try:
                result = str(eval(self.display.text()))
            except:
                result = 'Error!'
            self.display.setText(result)
        elif key == 'C':
            self.display.clear()
        # 버튼의 개수가 늘어나면 좋은 코드가 아님, 반복이 심함
        elif key in constantDic.keys():
            self.display.setText(self.display.text() + constantDic[key])
        elif key in functionDic.keys():
            n = self.display.text()
            value = eval('calcFunctions.' + functionDic[key] + '(n)')
            self.display.setText((str(value)))

        # elif key == constantList[0]:
        #     self.display.setText(self.display.text() + '3.141592')
        # elif key == constantList[1]:
        #     self.display.setText(self.display.text() + '3E+8')
        # elif key == constantList[2]:
        #     self.display.setText(self.display.text() + '340')
        # elif key == constantList[3]:
        #     self.display.setText(self.display.text() + '1.5E+8')
        # elif key == functionList[0]:
        #     n = self.display.text()
        #     value = calcFunctions.factorial(n)
        #     self.display.setText(str(value))
        # elif key == functionList[1]:
        #     n = self.display.text()
        #     value = calcFunctions.decToBin(n)
        #     self.display.setText(str(value))
        # elif key == functionList[2]:
        #     n = self.display.text()
        #     value = calcFunctions.binToDec(n)
        #     self.display.setText(str(value))
        # elif key == functionList[3]:
        #     n = self.display.text()
        #     value = calcFunctions.decToRoman(n)
        #     self.display.setText(str(value))
        else:
            self.display.setText(self.display.text() + key)