19

funcion单击时我试图调用函数btn_brow_3。我怎么能做到这一点?

运行以下代码时,我收到此错误消息: TypeError: connect() slot argument should be a callable or a signal not "nonetype"

import atexit
import sys
from PyQt4 import QtGui
import os                           # Libreria para manejar directorios del sistema operativo

def funcion(a):
    print "Hola mundo" + str(a)

class Example(QtGui.QMainWindow):

    def __init__(self):
        super(Example, self).__init__()
        
        self.initUI()
        
    def initUI(self):               
        
        exitAction = QtGui.QAction(QtGui.QIcon('c:/prueba gui/resource/logo.png'), 'Exit', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.triggered.connect(QtGui.qApp.quit)
        
        btn_brow_1 = QtGui.QPushButton('Browser...', self)
        btn_brow_1.resize(btn_brow_1.sizeHint())
        btn_brow_1.move(300, 50)
        btn_brow_1.clicked.connect(self.showDialog_points)
        
        btn_brow_2 = QtGui.QPushButton('Dir browser', self)
        btn_brow_2.resize(btn_brow_2.sizeHint())
        btn_brow_2.move(300, 80)
        btn_brow_2.clicked.connect(self.showDialog_indir_stl)
        
        btn_brow_3 = QtGui.QPushButton('Dir browser', self)
        btn_brow_3.resize(btn_brow_3.sizeHint())
        btn_brow_3.move(300, 110)
        btn_brow_3.clicked.connect(self.showDialog_outdir_stl)
        
        
        
        
        btn_brow_4 = QtGui.QPushButton('Crear soportes', self)
        btn_brow_4.setGeometry(20, 145, 250, 25)
        #btn_brow_4.clicked.connect(support.main(fname_points, self.fname_stl_indir, self.fname_stl_outdir))
        btn_brow_4.clicked.connect(funcion(12))  # HERE IS THE PROBLEM!
        
        self.le1 = QtGui.QLineEdit(self)
        self.le1.setGeometry(20, 50, 250,21)
        
        self.le2 = QtGui.QLineEdit(self)
        self.le2.setGeometry(20, 80, 250,21)
        
        self.le3 = QtGui.QLineEdit(self)
        self.le3.setGeometry(20, 110, 250,21)
        
        self.statusBar().showMessage("Ready")
        
        self.toolbar = self.addToolBar('Exit')
        self.toolbar.addAction(exitAction)
        
        self.setGeometry(300, 300, 400, 200)
        self.setWindowTitle('Support from points generator')    
        self.show()
        
        
    
    def showDialog_points(self):
        
        self.fname_points = QtGui.QFileDialog.getOpenFileName(self, 'Open points file', '/home')
        self.statusBar().showMessage(str(self.fname_points))
        self.le1.setText(str(self.fname_points))
        self.fname_points = str(self.le1.text())
        print fname_points

    
    def showDialog_indir_stl(self):
        
        self.fname_stl_indir = QtGui.QFileDialog.getExistingDirectory(self, 'Select STL INPUT directory', '/home')
        self.statusBar().showMessage(str(self.fname_stl_indir))
        self.le2.setText(str(self.fname_stl_indir))
        self.fname_stl_indir = str(self.le2.text())
        print fname_stl_indir 

    def showDialog_outdir_stl(self):
        
        self.fname_stl_outdir = QtGui.QFileDialog.getExistingDirectory(self, 'Select STL OUTPUT directory', '/home')
        self.statusBar().showMessage(str(self.fname_stl_outdir))
        self.le3.setText(str(self.fname_stl_outdir))
        self.fname_stl_outdir = str(self.le3.text())
        print fname_stl_outdir 
  
def main():
    
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    
    
    sys.exit(app.exec_())
    
if __name__ == '__main__':
    main()
4

3 回答 3

28

Qt 信号可以有参数,这些参数将传递给它们所连接的插槽;一个例子是将新值作为信号中的参数changed。因此,虽然您可以有一个带参数的插槽,但您不能在将信号连接到插槽时定义这些参数的实际值,因为它们将在发出信号时定义。

为了在连接时定义一个参数,您可以使用一个附加函数,它只使用定义的参数调用原始函数:

def wrapper():
    funcion(12)

def funcion(a):
    print "Hola mundo" + str(a)

[...]
btn_brow_4.clicked.connect(wrapper)

附带说明:wrapper此处不使用大括号:不调用该函数,而只是将其作为参数传递给该函数connect。在您的代码中,您调用了您的函数funcion,它什么都不返回 (= None),它在您的原始代码中被传递connect,导致您收到错误消息。

为了使它更简洁,您还可以使用匿名函数:

btn_brow_4.clicked.connect(lambda: funcion(12))

请注意,Qt 也提供了执行此操作的方法,但是(至少对我而言)Python 变体更易于阅读。

编辑:更多信息:http ://eli.thegreenplace.net/2011/04/25/passing-extra-arguments-to-pyqt-slot/

于 2013-02-26T09:12:41.723 回答
8

尝试这个,

def funcion(a):
 print "Hola mundo" + str(a)

[...]
btn_brow_4.clicked.connect(lambda: funcion(12))
于 2018-08-20T05:42:06.527 回答
5

您可以使用以下方法访问相同的内容:

self.connect(btn_brow_4,Qt.SIGNAL("clicked()"),self.callingFunction)

def callingFunction():
    function(12)
于 2013-02-26T09:20:38.300 回答