PyQt 如何将按钮连接到具有特定参数的函数?

How in PyQt connect button to a function with a specific parameter?

我的想法是,当按下按钮时,我需要启动一个函数,该函数的参数是该按钮的文本。

# -*- coding: utf-8 -*-
import ftplib 
from PyQt4 import QtGui, QtCore
import sys
import socket

app = QtGui.QApplication(sys.argv)
window = QtGui.QWidget()

List = ['one', 'two', 'free']

layer = QtGui.QVBoxLayout()
window.setLayout(layer)

def btn_clicked(btn):
    print 'button with text <%s> clicked' %(btn)

for i in List:
    button = QtGui.QPushButton(i)
    layer.addWidget(button)
    QtCore.QObject.connect(button, QtCore.SIGNAL('clicked()'),  btn_clicked(button.text())) # <--- the problem is here 

window.show()
sys.exit(app.exec_())

像这样连接按钮:

for i in List:
    button = QtGui.QPushButton(i)
    layer.addWidget(button)
    button.clicked.connect(lambda arg, text=i: myfunc(text))

与 lambda 相比,我更喜欢 partial,我认为这将易于使用。

from functools import partial
...
for i in List:
    button = QtGui.QPushButton(i)
    layer.addWidget(button)
    button.clicked.connect(partial(btn_clicked, str(button.text())))