PyQt 4 - 未定义全局名称“SIGNAL”

时间:2012-08-24 20:25:21

标签: python pyqt4

我正在尝试将按钮信号连接到我创建的可调用信号,但由于某种原因,此错误会一直弹出。我已经检查确保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

2 个答案:

答案 0 :(得分:7)

SIGNAL位于QtCore内,因此该行应为:

self.connect(self.qFileButton, QtCore.SIGNAL("pressed()"), self.loadFile)

但你真的应该使用the new style connections

self.qFileButton.pressed.connect(self.loadFile)

而且,除非您打算区分clickpress/release对,否则最好使用clicked信号:

self.qFileButton.clicked.connect(self.loadFile)

答案 1 :(得分:1)

SIGNALQtCore内定义,因此如果您整体导入QtCore,则必须在QtCore命名空间内使用它。所以使用:

QtCore.SIGNAL(...)

而不是:

SIGNAL(...)

或者您可以明确地从SIGNAL导入QtCore

from PyQt4.QtCore import SIGNAL