Skip to main content
 首页 » 编程设计

python之PyQt 4之全局名称 'SIGNAL' 未定义

2025年05月04日12oomusou

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

请您参考如下方法:

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)