gpt4 book ai didi

python - 子类化 : __init__() takes 1 positional argument but 2 were given

转载 作者:行者123 更新时间:2023-12-01 09:34:15 24 4
gpt4 key购买 nike

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

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

加载用户界面表

这样我就可以确保它正确加载,子类看起来就像这样

from PyQt5 import QtWidgets

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

除非我遗漏了什么,否则只会创建一个具有不同名称的 qgraphicsview。

但是,我收到错误

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

最佳答案

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)

正确的语法是:

super(AnotherDialog, self).__init__(parent, **kwargs)

关于python - 子类化 : __init__() takes 1 positional argument but 2 were given,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49675279/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com