__init__() 缺少 1 个必需的位置参数:'self'
__init__() missing 1 required positional argument: 'self'
我是 QT 的新手,我尝试使用 QT Designer 创建一个 Widget。
我现在正在尝试连接按钮,同时还使用继承从另一个编辑 Ui_Form class。
这是主文件:
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 编程比较陌生,对 Whosebug 也是新手,所以如果我做错了什么请不要杀了我 lmao
(哦,为了澄清与连接语句的任何混淆 = 这些函数存在,我只是将它们从这段代码中删除,因为我认为它们不重要(因为它们不是问题所在))
看起来有两个交换参数。
而不是:
class PythonGame(QtWidgets.QWidget, Ui_Form):
def __init__(PythonGame, self):
应该是:
class PythonGame(QtWidgets.QWidget, Ui_Form):
def __init__(self, PythonGame):
但是 PythonGame
是你的 class 名字,可能不应该在那里,不是吗?
这样就可以了
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)
__init__()
中的 self
是对自身实例的引用,并作为 class 方法的第一个参数放置。可以指定额外的参数。在您所拥有的内容中,PythonGame
用作传统上的 self
并将 self
视为另一个参数。去掉 PythonGame
参数,你应该没问题。因为它是一个 class 方法,它知道它是 PythonGame
的一部分
def __init__(self):
Often, the first argument of a method is called self. This is nothing more than a convention: the name self has absolutely no special meaning to Python. Note, however, that by not following the convention your code may be less readable to other Python programmers, and it is also conceivable that a class browser program might be written that relies upon such a convention.
PythonGame
的__init__
函数是错误的,应该是:
class PythonGame(QtWidgets.QWidget, Ui_Form):
def __init__(self, parent=None):
QWidget.__init__(self, parent=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)
你的外在方法不对。您应该只为任何 class 属性传递 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)
我是 QT 的新手,我尝试使用 QT Designer 创建一个 Widget。 我现在正在尝试连接按钮,同时还使用继承从另一个编辑 Ui_Form class。
这是主文件:
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 编程比较陌生,对 Whosebug 也是新手,所以如果我做错了什么请不要杀了我 lmao
(哦,为了澄清与连接语句的任何混淆 = 这些函数存在,我只是将它们从这段代码中删除,因为我认为它们不重要(因为它们不是问题所在))
看起来有两个交换参数。 而不是:
class PythonGame(QtWidgets.QWidget, Ui_Form):
def __init__(PythonGame, self):
应该是:
class PythonGame(QtWidgets.QWidget, Ui_Form):
def __init__(self, PythonGame):
但是 PythonGame
是你的 class 名字,可能不应该在那里,不是吗?
这样就可以了
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)
__init__()
中的 self
是对自身实例的引用,并作为 class 方法的第一个参数放置。可以指定额外的参数。在您所拥有的内容中,PythonGame
用作传统上的 self
并将 self
视为另一个参数。去掉 PythonGame
参数,你应该没问题。因为它是一个 class 方法,它知道它是 PythonGame
def __init__(self):
Often, the first argument of a method is called self. This is nothing more than a convention: the name self has absolutely no special meaning to Python. Note, however, that by not following the convention your code may be less readable to other Python programmers, and it is also conceivable that a class browser program might be written that relies upon such a convention.
PythonGame
的__init__
函数是错误的,应该是:
class PythonGame(QtWidgets.QWidget, Ui_Form):
def __init__(self, parent=None):
QWidget.__init__(self, parent=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)
你的外在方法不对。您应该只为任何 class 属性传递 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)