gpt4 book ai didi

python - 在 __init__ 调用中使用 self.tr 时出现 "RuntimeError: super-class __init__() of %S was never called"

转载 作者:行者123 更新时间:2023-11-30 23:28:20 27 4
gpt4 key购买 nike

在 PyQt 的代码中,当 self.tr 用于调用祖先类的 init 时,产生错误。没有 self.tr 的调用有效。见下文:

import sys
from PyQt4 import QtGui

class cl1(QtGui.QWidget):
def __init__(self,txt):
super(cl1,self).__init__()
self.edit = QtGui.QLineEdit(txt)
lay = QtGui.QVBoxLayout()
lay.addWidget(self.edit)
self.setLayout(lay)
self.show()

class cl2(cl1):
def __init__(self):
# This line does not work:
super(cl2,self).__init__(self.tr("kuku"))
# If this line is used instead, it works:
# super(cl2,self).__init__("kuku")

app = QtGui.QApplication(sys.argv)
w = cl2()
sys.exit(app.exec_())

最佳答案

正如已经指出的,在初始化之前不能调用基类的方法。

解决此问题的一种方法是使用静态 QApplication.translate方法(PyQt 不提供静态 QObject.tr 方法):

    super(cl2,self).__init__(QtGui.QApplication.translate("cl2", "kuku"))

关于python - 在 __init__ 调用中使用 self.tr 时出现 "RuntimeError: super-class __init__() of %S was never called",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21671932/

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