PyQt 4 - 未定义全局名称“SIGNAL" [英] PyQt 4 - global name 'SIGNAL' is not defined

查看:22
本文介绍了PyQt 4 - 未定义全局名称“SIGNAL"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将按钮信号连接到我创建的可调用对象,但由于某种原因,此错误不断弹出.我已经检查以确保 QtCore 已导入……我还缺少什么?

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?

示例代码:

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

推荐答案

SIGNALQtCore 里面,所以这行应该是:

SIGNAL is inside QtCore, so the line should be:

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

但您确实应该使用新样式连接:

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

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

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

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

这篇关于PyQt 4 - 未定义全局名称“SIGNAL"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆