pyqt4 - global name 'SIGNAL'没有定义。

时间:2020-12-03 16:48:33

I am trying to connect a push button signal to a callable I created, but for some reason this error keeps on popping up. I've checked to make sure QtCore is imported ... what else am I missing?

我试图将一个按钮信号连接到我创建的callable,但是由于某些原因,这个错误不断出现。我查了一下,确保QtCore是进口的……我还漏掉了什么?

Sample code:

示例代码:

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 个解决方案

#1


7  

SIGNAL is inside QtCore, so the line should be:

信号在QtCore内部,所以线路应该是:

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

but you really should use the new style connections:

但是你真的应该使用新的风格连接:

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

And, unless you meant to differentiate a click from press/release couple, you'd better use clicked signal:

而且,除非你想让你的点击与按下/发布的那对不同,你最好使用点击的信号:

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

#2


1  

SIGNAL is defined inside QtCore, so you must use it within QtCore namespace if you've imported QtCore as a whole. So use:

信号是在QtCore中定义的,所以如果您将QtCore作为一个整体导入,那么您必须在QtCore命名空间中使用它。所以使用:

QtCore.SIGNAL(...)

instead of:

而不是:

SIGNAL(...)

Or you can import SIGNAL from QtCore explicitly:

或者您可以明确地从QtCore导入信号:

from PyQt4.QtCore import SIGNAL

#1


7  

SIGNAL is inside QtCore, so the line should be:

信号在QtCore内部,所以线路应该是:

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

but you really should use the new style connections:

但是你真的应该使用新的风格连接:

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

And, unless you meant to differentiate a click from press/release couple, you'd better use clicked signal:

而且,除非你想让你的点击与按下/发布的那对不同,你最好使用点击的信号:

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

#2


1  

SIGNAL is defined inside QtCore, so you must use it within QtCore namespace if you've imported QtCore as a whole. So use:

信号是在QtCore中定义的,所以如果您将QtCore作为一个整体导入,那么您必须在QtCore命名空间中使用它。所以使用:

QtCore.SIGNAL(...)

instead of:

而不是:

SIGNAL(...)

Or you can import SIGNAL from QtCore explicitly:

或者您可以明确地从QtCore导入信号:

from PyQt4.QtCore import SIGNAL