1

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

2 回答 2

7

SIGNAL在里面QtCore,所以该行应该是:

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

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

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

而且,除非你打算区分一clickpress/release夫妇,否则你最好使用clicked信号:

self.qFileButton.clicked.connect(self.loadFile)
于 2012-08-24T20:35:30.960 回答
1

SIGNAL是在内部定义的QtCore,所以QtCore如果你QtCore作为一个整体导入,你必须在命名空间中使用它。所以使用:

QtCore.SIGNAL(...)

代替:

SIGNAL(...)

或者您可以显式导入SIGNALQtCore

from PyQt4.QtCore import SIGNAL
于 2012-08-24T20:30:39.263 回答