子类化:__ init __()占用1个位置参数,但给出了2个

时间:2018-04-05 14:47:47

标签: python pyqt5

我正在尝试子类化内置的Qt ui类。在QtDesigner中,我将QGraphicsView“提升”为“ResultView”,并在加载UI的类中添加一个include并使用

uic.loadUi('mainwindow.ui', self)

加载ui表

这样我可以确保正确加载子类只看起来像这个

from PyQt5 import QtWidgets

class ResultView(QtWidgets.QGraphicsView):
    def __init__(self):
        super(ResultView, self).__init__()

除非我遗漏了某些内容,否则只会创建一个名称不同的qgraphics视图。

但是,我收到了错误

TypeError: __init__() takes 1 positional argument but 2 were given

我发送的第二个论点是什么?

降级到qgraphicsview修复了错误,所以我很确定它在这个类中导致它。

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2.2\helpers\pydev\pydevd.py", line 1596, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2.2\helpers\pydev\pydevd.py", line 1023, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/swalsh/Desktop/Mandelbrot/main.py", line 89, in <module>
    window = MainWindow()
  File "C:/Users/swalsh/Desktop/Mandelbrot/main.py", line 15, in __init__
    uic.loadUi('mainwindow.ui', self)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\__init__.py", line 226, in loadUi
    return DynamicUILoader(package).loadUi(uifile, baseinstance, resource_suffix)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\Loader\loader.py", line 72, in loadUi
    return self.parse(filename, resource_suffix, basedir)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 1000, in parse
    actor(elem)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 807, in createUserInterface
    self.traverseWidgetTree(elem)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 785, in traverseWidgetTree
    handler(self, child)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 262, in createWidget
    self.traverseWidgetTree(elem)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 785, in traverseWidgetTree
    handler(self, child)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 482, in createLayout
    self.traverseWidgetTree(elem)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 785, in traverseWidgetTree
    handler(self, child)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 525, in handleItem
    self.traverseWidgetTree(elem)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 785, in traverseWidgetTree
    handler(self, child)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 262, in createWidget
    self.traverseWidgetTree(elem)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 785, in traverseWidgetTree
    handler(self, child)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 262, in createWidget
    self.traverseWidgetTree(elem)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 785, in traverseWidgetTree
    handler(self, child)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 482, in createLayout
    self.traverseWidgetTree(elem)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 785, in traverseWidgetTree
    handler(self, child)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 525, in handleItem
    self.traverseWidgetTree(elem)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 785, in traverseWidgetTree
    handler(self, child)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 262, in createWidget
    self.traverseWidgetTree(elem)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 785, in traverseWidgetTree
    handler(self, child)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 262, in createWidget
    self.traverseWidgetTree(elem)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 785, in traverseWidgetTree
    handler(self, child)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 482, in createLayout
    self.traverseWidgetTree(elem)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 785, in traverseWidgetTree
    handler(self, child)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 525, in handleItem
    self.traverseWidgetTree(elem)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 785, in traverseWidgetTree
    handler(self, child)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 262, in createWidget
    self.traverseWidgetTree(elem)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 785, in traverseWidgetTree
    handler(self, child)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 262, in createWidget
    self.traverseWidgetTree(elem)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 785, in traverseWidgetTree
    handler(self, child)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 482, in createLayout
    self.traverseWidgetTree(elem)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 785, in traverseWidgetTree
    handler(self, child)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 525, in handleItem
    self.traverseWidgetTree(elem)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 785, in traverseWidgetTree
    handler(self, child)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 253, in createWidget
    self.stack.push(self.setupObject(widget_class, parent, elem))
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\uiparser.py", line 217, in setupObject
    obj = self.factory.createQObject(clsname, name, args, is_attribute)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\objcreator.py", line 124, in createQObject
    return self._cpolicy.instantiate(factory, *args, **kwargs)
  File "C:\Users\swalsh\Anaconda3\lib\site-packages\PyQt5\uic\Loader\qobjectcreator.py", line 139, in instantiate
    return clsObject(*ctor_args)
TypeError: __init__() takes 1 positional argument but 2 were given

1 个答案:

答案 0 :(得分:0)

int

from PyQt5 import QtWidgets

class ResultView(QtWidgets.QGraphicsView):
    def __init__(self, parent):
        super(ResultView, self).__init__(parent)

正确的语法是:

from PyQt5 import QtWidgets

class ResultView(QtWidgets.QGraphicsView):
    def __init__(self):
        super(ResultView, self).__init__(self.parent)