英文:
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论