0
    from PyQt5.QtGui import *
from PyQt5.QtWidgets import*
from PyQt5.QtCore import *

import operator

from Calculator import Ui_MainWindow

# Calculator state.
READY = 0
INPUT = 1

class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self),__init__(*args, **kwargs)
        self.setupUi(self)

        # setup numbers
        for n in range(0, 10):
            getattr(self, 'pushButton_nks' % n).pressed.connect(lambda v=n: self.input_number(v))

        # setup operation
        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)

        # setup actions
        self.actionReset.triggered.connect(self.reset)
        self.pushButton_ac.pressed.connect(self.reset)

        self.actionExit.triggered.connect(self.close)

        self.pushButton_m.pressed.connect(self.memory_store)
        self.pushButton_mr.pressed.connect(self.memory_recall)

        self.memory = 0
        self.reset()

        self.show()

    def display(self):
        self.lodNumber.display(self.stack[-1])

    def reset(self):
        self.state = READY
        self.stack = [0]
        self.last_operation = None
        self.current_op = None
        self.display()

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

    def memory_recall(self):
        self.state = INPUT
        self.stack[-1] = self.memory
        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: #complete the current operation
            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):
        #support to allow '=' tp repeat previous operation
        #if no futher input has been added.
        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.lodNumber.display('Err')
                self.stack = [0]
            else:
                self.current_op = None
                self.state = READY
                self.display()

if __name__ == '__main__':
    app = QApplication([])
    app.setApplicationName("calculon")

    window = MainWindow()
    app.exec_()

when I run this, I come across a problem that I can't solve on my own, I hope to find the answer here

Traceback (most recent call last):

File "C:/Python35/Lib/site-packages/PyQt5/qt ddesain/callKalkulator.py", line 109, in window = MainWindow() File "C:/Python35/Lib/site-packages/PyQt5/qt ddesain/callKalkulator.py", line 15, in init super(MainWindow, self),init(*args, **kwargs) NameError: name 'init' is not defined

i got this kind of problem with my code like this, is there any way to solve it

1
  • 3
    That comma needs to be a period. Commented Dec 24, 2020 at 18:20

1 Answer 1

1

It's a clear error in syntax for calling the constructor of parent class:

super(MainWindow, self).__init__(*args, **kwargs)

Note than in python3, you can simply use super().__init__(*args, **kwargs).

Sign up to request clarification or add additional context in comments.

15 Comments

Well, it's not a syntax error. Otherwise it would raise a SyntaxError.
I clearly believe I didn't user the exact wording SyntaxError either. @Chris Modified the answer anyway.
You don't have to have said SyntaxError. If it's syntactically valid it isn't a syntax error. This is a logic error, or a typo, however you want to look at it.
can you give me an example
What do you mean by example? Just replace , by . in your code as mentioned in the answer @Tegarrangga
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.