一些环境基础知识 python 版本:3.4.2 操作系统:Windows 8.1
搜索到目前为止,我怀疑this other question与我手头的问题有关,但我不确定我是如何复制足够多的相同条件的——可能是我缺乏深入的 python 知识。
重现问题的简化代码:
基类
from PySide.QtGui import *
class Interface(QWidget):
'''
Wrapper base class for GUI input QWidgets:
- buttons
- text fields
- checkboxes
- line edit
- dropdown menu (combo box)
'''
def __init__(self, parent, name, title_txt=None, qt_obj=None,
update_log_method=None):
print('Interface base class constructor has been called.') #DEBUG
self._parent = parent
self.title = None
self.name = name #also text that appears on component
self.qt_obj = qt_obj
self.inheritted_log_method = update_log_method
# don't want to create an empty text QLabel, or one with
# the text reading "None".
if title_txt:
self.title = QLabel(text=title_txt, parent=parent)
print('Interface base class constructor has been completed.') #DEBUG
def get_name(self):
return self.name
def update_log(self, message, level="INFO"):
''' '''
self.inheritted_log_method(message, level)
继承类
class IFPushButton(Interface):
''' '''
def __init__(self, name, parent, icon=None, update_log_method=None):
''' '''
# print('\n\nCHECKPOINT: pre IFPushButton super()\n\n') #DEBUG
super(IFPushButton, self).__init__(
parent=parent,
name=name,
qt_obj=QPushButton(icon, name, parent),
update_log_method=update_log_method)
self.behaviors = {}
self.qt_obj.clicked.connect(self.activate)
一切开始的东西
if __name__ == '__main__':
# setup
import sys
app = QApplication(sys.argv)
qmw = QMainWindow()
qcw = QWidget() #central widget
qcl = QVBoxLayout(qcw) #central layout
# experimental
name = 'named button'
ifpb = IFPushButton(name=name, parent=None, icon=None, update_log_method=None)
print("as long a I don't touch the ifpb instance, everything seems to be okay.")
print("...but the second I do...")
qcl.addWidget(ifpb)
self.show()
print("name of created push button:", ifpb.get_name())
# proper teardown
sys.exit(app.exec_())
我在一个模块 interface.py
中运行这一切,当我运行它时......
C:\Path\To\Module> python interface.py
Interface base class constructor has been called.
Interface base class constructor has been completed.
as long a I don't touch the ifpb instance, everything seems to be okay.
...but the second I do...
Traceback (most recent call last):
File "c_interface.py", line 167, in <module>
qcl.addWidget(ifpb)
RuntimeError: '__init__' method of object's base class (IFPushButton) not called.
让我感到困惑的部分是基类 Intefrace
中的打印语句在打印时显然是如何被调用的——但它仍然引发一个 RuntimeError 说它还没有被调用初始化,当然无法创建应用程序窗口。我在 stackoverflow 上发现的大多数相关消息都与人们使用 super() 方法错误地初始化事物有关——但我已经对我的 super 初始化进行了五次检查,我看到的一切都告诉我它应该工作,除了我上面链接的内容。
如果我能更多地了解为什么会发生这种情况,我希望我能找到解决它的方法。非常感谢任何帮助 - 提前致谢!
与此同时,我将尝试找出我可能无意中深度复制 C++ 对象的原因...
编辑:在指向其他堆栈溢出帖子的链接中包含 url。
最佳答案
需要向 Interface
类构造函数添加一个 super
调用:
def __init__(self, parent, name, title_txt=None, qt_obj=None, update_log_method=None):
super(Interface, self).__init__(parent)
...
此外,您正在调用 self.show()
,您可能指的是 qmw.show()
。
关于python - PySide.QtGui RuntimeError : '__init__' method of object's base class not called . ..但它是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30114436/