为啥 super(MainWindow, self),__init__(*args, **kwargs) NameError: name '__init__' is not defined

Posted

技术标签:

【中文标题】为啥 super(MainWindow, self),__init__(*args, **kwargs) NameError: name \'__init__\' is not defined【英文标题】:why does super(MainWindow, self),__init__(*args, **kwargs) NameError: name '__init__' is not defined为什么 super(MainWindow, self),__init__(*args, **kwargs) NameError: name '__init__' is not defined 【发布时间】:2020-12-24 18:17:26 【问题描述】:
    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_()

当我运行这个时,我遇到了一个我自己无法解决的问题,我希望在这里找到答案

Traceback (most recent call last):

文件“C:/Python35/Lib/site-packages/PyQt5/qt ddesain/callKalkulator.py”,第 109 行,在 窗口 = 主窗口() init 中的文件“C:/Python35/Lib/site-packages/PyQt5/qt ddesain/callKalkulator.py”,第 15 行 super(MainWindow, self),init(*args, **kwargs) NameError: name 'init' 未定义

我的代码遇到了这样的问题,有什么办法可以解决它

【问题讨论】:

逗号必须是句号。 【参考方案1】:

调用父类的构造函数的语法明显错误:

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

注意,与python3相比,你可以简单地使用super().__init__(*args, **kwargs)

【讨论】:

嗯,这不是 语法 错误。否则它会引发SyntaxError 我显然相信我没有使用确切的措辞 SyntaxError。 @Chris 还是修改了答案。 你不必说SyntaxError。如果它在语法上有效,则不是语法错误。这是一个逻辑错误,或者是一个错字,但是你想看看它。 你能举个例子吗 你的例子是什么意思?只需将, 替换为@Tegarrangga 答案中提到的代码中的. 即可

以上是关于为啥 super(MainWindow, self),__init__(*args, **kwargs) NameError: name '__init__' is not defined的主要内容,如果未能解决你的问题,请参考以下文章

为啥 Python 3.x 的 super() 有魔力?

扩展标签字符

无法从 PyQT5 文本字段中获取文本

为啥__class__可以直接访问[重复]

pyside2-uic 工具把ui文件转化为python类

Swift 两种UIImageView写法,为啥一种显示得出?