__init __()缺少1个必需的位置参数:“ self”

时间:2019-04-28 18:32:59

标签: python qt widget pyqt5 qwidget

我是QT的新手,我尝试使用QT Designer创建小部件。 我现在尝试连接按钮,同时也使用继承从另一个编辑Ui_Form类。

这是主文件:

from PyQt5 import QtCore, QtGui, QtWidgets, uic
from HunterVsBoar import Ui_Form


class PythonGame(QtWidgets.QWidget, Ui_Form):
    def __init__(PythonGame, self):
        def __init__(self, parent = None):
            super(PythonGame, self).__init__(parent)
            self.ui = Ui_Form()
            self.ui.setupUi(self)

            self.attack_btn.clicked.connect(self, self.start_attack)
            self.heal_btn.clicked.connect(self, self.heal_hunter)
            self.start_btn.clicked.connect(self, self.event_start)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = PythonGame()
    Form.show()
    sys.exit(app.exec_())

这是转换后的ui文件:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Form(object):


    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(1037, 885)
        self.horizontalLayoutWidget = QtWidgets.QWidget(Form)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(0, 819, 1031, 61))
        self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.attack_btn = QtWidgets.QPushButton(self.horizontalLayoutWidget)
        font = QtGui.QFont()
        font.setFamily("Yu Gothic UI Semibold")
        font.setBold(True)
        font.setWeight(75)

        self.attack_btn.setFont(font)
        self.attack_btn.setObjectName("attack_btn")
        self.horizontalLayout.addWidget(self.attack_btn)

        self.heal_btn = QtWidgets.QPushButton(self.horizontalLayoutWidget)
        self.heal_btn.setCheckable(False)
        self.heal_btn.setObjectName("heal_btn")
        self.horizontalLayout.addWidget(self.heal_btn)

        self.dmg_btn = QtWidgets.QPushButton(self.horizontalLayoutWidget)
        self.dmg_btn.setObjectName("dmg_btn")
        self.horizontalLayout.addWidget(self.dmg_btn)

        self.verticalLayoutWidget = QtWidgets.QWidget(Form)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(0, 0, 1041, 311))
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")

        self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")

        self.textBrowser_2 = QtWidgets.QTextBrowser(self.verticalLayoutWidget)
        self.textBrowser_2.setObjectName("textBrowser_2")
        self.verticalLayout.addWidget(self.textBrowser_2)

        self.textBrowser_1 = QtWidgets.QTextBrowser(self.verticalLayoutWidget)
        self.textBrowser_1.setObjectName("textBrowser_1")
        self.verticalLayout.addWidget(self.textBrowser_1)

        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(440, 790, 131, 16))
        self.label.setObjectName("label")

        self.hp_hunter = QtWidgets.QProgressBar(Form) 
        self.hp_hunter.setGeometry(QtCore.QRect(10, 790, 118, 23))
        self.hp_hunter.setProperty("value", 100)
        self.hp_hunter.setObjectName("hp_hunter")

        self.label_2 = QtWidgets.QLabel(Form)
        self.label_2.setGeometry(QtCore.QRect(40, 770, 47, 13))
        self.label_2.setObjectName("label_2")

        self.hp_boar = QtWidgets.QProgressBar(Form)
        self.hp_boar.setGeometry(QtCore.QRect(900, 790, 118, 23))
        self.hp_boar.setProperty("value", 100)
        self.hp_boar.setObjectName("hp_boar")

        self.label_3 = QtWidgets.QLabel(Form)
        self.label_3.setGeometry(QtCore.QRect(930, 770, 47, 13))
        self.label_3.setObjectName("label_3")

        self.start_btn = QtWidgets.QPushButton(Form)
        self.start_btn.setGeometry(QtCore.QRect(370, 630, 75, 23))
        self.start_btn.setObjectName("start_btn")

        self.stop_btn = QtWidgets.QPushButton(Form)
        self.stop_btn.setGeometry(QtCore.QRect(520, 630, 75, 23))
        self.stop_btn.setObjectName("stop_btn")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Hunter vs Boar"))
        self.attack_btn.setText(_translate("Form", "Attack"))
        self.heal_btn.setText(_translate("Form", "Heal"))
        self.dmg_btn.setText(_translate("Form", "Damage potion"))
        self.label.setText(_translate("Form", "What do you want to do?"))
        self.label_2.setText(_translate("Form", "Hunter"))
        self.label_3.setText(_translate("Form", "Boar"))
        self.start_btn.setText(_translate("Form", "Start!"))
        self.stop_btn.setText(_translate("Form", "Stop!"))

启动main.py后,我会收到以下错误消息:

__init__() missing 1 required positional argument: 'self'

在标记Form = PythonGame()时。

有人可以在这里帮助我吗?就像我说的那样,我是Python编程的新手,也是Stackoverflow的新手,所以如果我做错了lmao,请不要杀死我

(哦,为了澄清与connect语句的混淆=这些函数存在,我只是从代码中删除了它们,因为我认为它们并不重要(因为它们不是问题))

4 个答案:

答案 0 :(得分:0)

self中的__init__()是对自身实例的引用,并作为类方法的第一个参数放置。可以指定其他参数。在您拥有的东西中,PythonGame就像传统的self一样,它正在将self视为另一个参数。摆脱PythonGame参数,你应该没事。由于它是一个类方法,因此它知道它是PythonGame

的一部分
def __init__(self):

documentation

  

通常,方法的第一个参数称为self。这无非是一种约定:self对Python绝对没有特殊的含义。但是请注意,如果不遵循该约定,则其他Python程序员可能对代码的可读性较低,并且还可以想到可能会依赖此类约定编写类浏览器程序。

答案 1 :(得分:0)

__init__的{​​{1}}函数是错误的,应该是:

PythonGame

答案 2 :(得分:0)

您的外部self方法不正确。您应该只为任何类属性传递self和任何必要的参数。

您的代码应如下所示:

class PythonGame(QtWidgets.QWidget, Ui_Form):
def __init__(self):
    def __init__(self, parent = None):
        super(PythonGame, self).__init__(parent)
        self.ui = Ui_Form()
        self.ui.setupUi(self)

答案 3 :(得分:-1)

看起来好像有两个交换的参数。 代替:

class PythonGame(QtWidgets.QWidget, Ui_Form):
    def __init__(PythonGame, self):

应该是:

class PythonGame(QtWidgets.QWidget, Ui_Form):
    def __init__(self, PythonGame):

但是PythonGame是您的班级名称,可能不应该在那儿,对吗? 那会做到

class PythonGame(QtWidgets.QWidget, Ui_Form):
    def __init__(self, parent = None):
        super(PythonGame, self).__init__(parent)
        self.ui = Ui_Form()
        self.ui.setupUi(self)

        self.attack_btn.clicked.connect(self, self.start_attack)
        self.heal_btn.clicked.connect(self, self.heal_hunter)
        self.start_btn.clicked.connect(self, self.event_start)