0

Ошибка в калькуляторе:

import sys
from PyQt5 import QtWidgets
from calc import Ui_MainWindow

class Win(QtWidgets.QMainWindow):
    def __init__(self):
        super(Win, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        for n in range(0, 10):
            getattr(self, 'pushButton_n"%"s' % n).pressed.connect(lambda v=n: self.input_number(v))

        self.pushButton_add.pressed.connect(lambda: self.operation(operator.add))
        self.pushButton_sub.pressed.connect(lambda: self.operation(operator.sub))
        self.pushButton_mul.pressed.connect(lambda: self.operation(operator.mul))
        self.pushButton_div.pressed.connect(lambda: self.operation(operator.truediv))

        self.pushButton_pc.pressed.connect(self.operation_pc)
        self.pushButton_eq.pressed.connect(self.equals)
        self.pushButton_ac.pressed.connect(self.reset)
        self.pushButton_m.pressed.connect(self.memory_store)
        self.pushButton_mr.pressed.connect(self.memory_recall)

    def input_number(self, v):
        if self.state == READY:
            self.state = INPUT
            self.stack[-1] = v
        else:
            self.stack[-1] = self.stack[-1] * 10 + v
        self.display()

    def input_number(self, v):
        if self.state == READY:
            self.state = INPUT
            self.stack[-1] = v
        else:
            self.stack[-1] = self.stack[-1] * 10 + v
        self.display() 

    def operation(self, op):
        if self.current_op:
            self.equals()

        self.stack.append(0)
        self.state = INPUT
        self.current_op = op

    def operation_pc(self):
        self.state = INPUT
        self.stack[-1] *= 0.01
        self.display()

    def equals(self):
        if self.state == READY and self.last_operation:
            s, self.current_op = self.last_operation
            self.stack.append(s)

        if self.current_op:
            self.last_operation = self.stack[-1], self.current_op

            try:
                self.stack = [self.current_op(*self.stack)]
            except Exception:
                self.lcdNumber.display('Error')
                self.stack = [0]
            else:
                self.current_op = None
                self.state = READY
                self.display()

    def memory_store(self):
        self.memory = self.lcdNumber.value()

    def memory_recall(self):
        self.state = INPUTself.stack[-1] = self.memory
        self.display()

app = QtWidgets.QApplication([])
application = Win()
application.show()

sys.exit(app.exec_())

Скорее всего ошибка в 12 строке внутри цикла for.

2
  • Вы забыли опубликовать модуль calc.py
    – S. Nick
    15 ноя 2019 в 21:36
  • Где ошибка написано в сообщении об ошибке. Так что не надо гадать, а надо привести его полностью. 15 ноя 2019 в 22:17

1 ответ 1

0

Не так закрыта кавычка, должно быть 'pushButton_n"%s"'.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.