Python 带有自定义插槽的“TypeError:原生 Qt 信号不可调用”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33049146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"TypeError: native Qt signal is not callable" with custom slots
提问by Xorgon
The Environment
环境
I am running an Anaconda environment with Python 3.4. I am using PyCharm as my IDE.
我正在使用 Python 3.4 运行 Anaconda 环境。我使用 PyCharm 作为我的 IDE。
The Objective
目标
I am trying to make a pyQt4 QPushButton connect to a custom function:
我正在尝试使 pyQt4 QPushButton 连接到自定义函数:
button.clicked().connect([method reference or whatever])
Attempts
尝试
I have tried using the pyqtSlot()
decoratorbut when I run the code it throws:
我曾尝试使用pyqtSlot()
装饰器,但是当我运行代码时,它会抛出:
NameError: name 'pyqtSlot' is not defined
I have used the following imports which should include that decorator:
我使用了以下应该包含该装饰器的导入:
from PyQt4 import QtCore, QtGui
I also attempted to change my method into its own callable class containing a call method.
我还尝试将我的方法更改为包含调用方法的自己的可调用类。
The general error message that I'm getting for various attempts is this:
我在各种尝试中得到的一般错误消息是:
TypeError: native Qt signal is not callable
The Question
问题
Honestly, at this point I have pretty much no idea where to go with this or what details you may need to diagnose the problem. Could anyone give me an idea how to put this together?
老实说,在这一点上,我几乎不知道该去哪里或者您可能需要哪些细节来诊断问题。谁能给我一个想法如何把它放在一起?
采纳答案by Orest Hera
pyqtSlot()
should be imported from PyQt4.QtCore
:
pyqtSlot()
应该从PyQt4.QtCore
:
from PyQt4.QtCore import pyqtSlot
it can be also used as @QtCore.pyqtSlot()
since you already import QtCore
.
它也可以用作@QtCore.pyqtSlot()
因为您已经导入了QtCore
.
You have the error message TypeError: native Qt signal is not callable
since the slot clicked
should be connected without parentheses:
您收到错误消息,TypeError: native Qt signal is not callable
因为插槽clicked
应该不带括号连接:
button.clicked.connect([method reference or whatever])
You can start from simple examples that you can find in PyQt4 package, for example examples/widgets/tetrix.py
:
您可以从可以在 PyQt4 包中找到的简单示例开始,例如examples/widgets/tetrix.py
:
startButton = QtGui.QPushButton("&Start")
startButton.clicked.connect(self.board.start)
回答by Anurag Singh
object.signal().connect(slot())
should be replaced with object.signal.connect(slot)
object.signal().connect(slot())
应该替换为 object.signal.connect(slot)