当按下按钮时调用一个函数 - pyqt [英] call a function when a button is pressed - pyqt

查看:255
本文介绍了当按下按钮时调用一个函数 - pyqt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手......当我点击btn_brow_3时,我会调用函数funcion我该怎么做?
有人可以帮我吗?

错误
TYPE ERROR connect()插槽参数应该是可调用的或者不是nonetype的信号



问候
Maxi

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

def funcion(a):
printHola mundo+ str(a)
$ b $ class示例(QtGui.QMainWindow):

def __init __(self):
super(Example,self).__ init __()

self.initUI()

def initUI(self):

exitAction = QtGui.QAction(QtGui.QIcon('c:/ prueba gui / resource / logo。 ('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))#这是问题!

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('支持点生成器')
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_分数


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
$ b $ def main():

app = QtGui.QApplication(sys.argv)
ex = Example()


sys.exit(app.exec_())

if __name__ =='__main__' :
main()


解决方案

有参数传递给他们连接的插槽;一个例子就是将新值作为信号改变中的一个参数。因此,虽然您可以拥有带参数的插槽,但您无法在将信号连接到插槽时定义这些插槽的实际值,因为它们将在发送信号时定义。



为了在连接时定义一个参数,你可以使用一个额外的函数,它除了用定义的参数调用原始函数之外什么也不做:

  def wrapper():
funcion(12)

def funcion(a):
printHola [b]

$ b btn_brow_4.clicked.connect(包装)

附注: wrapper 在这里不使用大括号:该函数未被调用,而是作为参数传递给函数 connect 。在你的代码中,你调用了你的函数 funcion ,它不返回任何东西(= None ) ,它被传递给原始代码中的 connect ,导致收到错误消息。



有点清洁,你也可以使用匿名函数:
$ b

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

请注意,Qt也提供了这样做的方法,但(至少对于我)Python变体更容易阅读。



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


I´m new in this... I would call function "funcion" when I click "btn_brow_3" How could I do that? Somebody can help me?

Error TYPE ERROR connect() slot argument should be a callable or a signal not "nonetype"

Regards Maxi

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()

解决方案

Qt signals can have arguments that will be passed to the slots they are connected to; an example would be the new value as an argument in a signal changed. Therefore, while you can have a slot with arguments, you cannot define the actual values of those when connecting the signal to the slot, as they will be defined when emitting signal.

For defining an argument at connect time, you can use an additional function which does nothing but calling the original function with the defined argument:

def wrapper():
    funcion(12)

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

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

As a side note: wrapper does not use braces here: the function is not called, but simply passed as an argument to the function connect. In your code, you called your function funcion, which returns nothing (=None), which was passed to connect in your original code, resulting in the error message you received.

To make that a bit cleaner, you can also use an anonymous function:

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

Note that Qt also provides ways of doing this, but (at least for me) the Python variants are easier to read.

Edit: Some more information: http://eli.thegreenplace.net/2011/04/25/passing-extra-arguments-to-pyqt-slot/

这篇关于当按下按钮时调用一个函数 - pyqt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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