我正在尝试将按钮信号连接到我创建的可调用信号,但由于某种原因,此错误会一直弹出。我已经检查确保QtCore已导入......我还缺少什么?
示例代码:
from PyQt4 import QtCore
from PyQt4 import QtGui
import sys
class guiFindFiles(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
#Create window
self.setFixedSize(400,180)
self.setWindowTitle("Choose the files to use")
#Create all layouts to be used by window
self.windowLayout = QtGui.QVBoxLayout()
self.fileLayout1 = QtGui.QHBoxLayout()
self.fileLayout2 = QtGui.QHBoxLayout()
self.fileLayout3 = QtGui.QHBoxLayout()
#Create the prompt for user to load in the q script to use
self.qFileTF = QtGui.QLineEdit("Choose the q script file to use")
self.qFileButton = QtGui.QPushButton("Open")
self.qFileButton.setFixedSize(100,27)
self.fileLayout1.addWidget(self.qFileTF)
self.fileLayout1.addWidget(self.qFileButton)
#Connect all the signals and slots
self.connect(self.qFileButton, SIGNAL("pressed()"), self.loadFile)
def loadFile():
fileName = []
selFile = QtGui.QFileDailog.getOpenFileName(self)
print selFile
答案 0 :(得分:7)
SIGNAL
位于QtCore
内,因此该行应为:
self.connect(self.qFileButton, QtCore.SIGNAL("pressed()"), self.loadFile)
但你真的应该使用the new style connections:
self.qFileButton.pressed.connect(self.loadFile)
而且,除非您打算区分click
与press/release
对,否则最好使用clicked
信号:
self.qFileButton.clicked.connect(self.loadFile)
答案 1 :(得分:1)
SIGNAL
在QtCore
内定义,因此如果您整体导入QtCore
,则必须在QtCore
命名空间内使用它。所以使用:
QtCore.SIGNAL(...)
而不是:
SIGNAL(...)
或者您可以明确地从SIGNAL
导入QtCore
:
from PyQt4.QtCore import SIGNAL