PyQt5无参数函数出错:参数1具有意外类型’NoneType’。

huangapple go评论62阅读模式
英文:

PyQt5 Function without arguments giving error: argument 1 has unexpected type 'NoneType'

问题

这是代码部分的翻译:

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys

class mainWindow(QWidget):
    def __init__(self):
        super(mainWindow, self).__init__()

        self.title = '窗口标题'
        self.left = 100
        self.top = 100
        self.width = 750
        self.height = 500
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        self.do_something()

        self.button_widget = QPushButton()
        self.layout = QGridLayout()
        self.layout.addWidget(self.button_widget, 0, 0)
        self.layout.addWidget(self.widgetX, 1, 0, 1, 3) # 行,列,行跨度,列跨度
        self.setLayout(self.layout)

        self.button_widget.clicked.connect(self.functionX)

    def do_something(self):
        self.widgetX = ClassX()
        self.functionX = self.widgetX.functionX()
        self.variableX = self.widgetX.variableX
	
class ClassX(QLabel):
    def __init__(self):
        super(ClassX, self).__init__()

        self.setFixedHeight(40)
        self.setMinimumWidth(600)
        self.setText('我是自定义的QLabel')
        self.variableX = 'X'

    def functionX(self):
        self.setText('我可以改变文本')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = mainWindow()
    ex.show()
    sys.exit(app.exec_())

请注意,我只翻译了代码部分,不包括评论或问题的其余部分。如果您有任何其他问题或需要进一步的帮助,请随时提出。

英文:

I am creating a highly generic template for PyQt5 applications. Without all the comments, here is the code so far. However, executing this code gives the following error:

self.button_widget.clicked.connect(self.functionX)
TypeError: argument 1 has unexpected type 'NoneType'

I am confused about this because the function takes no arguments other than self. What is going on here?

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys

class mainWindow(QWidget):
    def __init__(self):
        super(mainWindow, self).__init__()

        self.title = 'Window Title'
        self.left = 100
        self.top = 100
        self.width = 750
        self.height = 500
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        self.do_something()

        self.button_widget = QPushButton()
        self.layout = QGridLayout()
        self.layout.addWidget(self.button_widget, 0, 0)
        self.layout.addWidget(self.widgetX, 1, 0, 1, 3) # row, col, row_span, col_span
        self.setLayout(self.layout)

        self.button_widget.clicked.connect(self.functionX)

    def do_something(self):
        self.widgetX = ClassX()
        self.functionX = self.widgetX.functionX()
        self.variableX = self.widgetX.variableX
	
class ClassX(QLabel):
    def __init__(self):
        super(ClassX, self).__init__()

        self.setFixedHeight(40)
        self.setMinimumWidth(600)
        self.setText('I am a Custom QLabel')
        self.variableX = 'X'

    def functionX(self):
        self.setText('I can change text')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = mainWindow()
    ex.show()
    sys.exit(app.exec_())

I originally tried using generic arguments like args=None, but this resulted in the same issue. I believe something is going on with using self in both the mainWindow class and the ClassX class. This is NOT the same question as others that ask about this TypeError being given when using a function that takes an argument (like a string or other data type, not self).

答案1

得分: 2

你正在调用 `functionX`,它将返回 `None`。要赋值给函数,只需删除括号。

self.functionX = self.widgetX.functionX


<details>
<summary>英文:</summary>

self.functionX = self.widgetX.functionX()

You are calling `functionX` which will give you `None`. To assign function, you just have to remove parenthesis.

self.functionX = self.widgetX.functionX


</details>



huangapple
  • 本文由 发表于 2023年7月7日 04:02:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76632207.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定