当我运行下面的代码时,它显示如下。为什么 x 不是“x”而是变成布尔值?这种情况仅发生在传递到用 lambda 调用的函数中的第一个参数上。
错误的
y
/home/me/model/some_file
from PyQt5.QtWidgets import QPushButton
modelpath = '/home/me/model'
filelist = os.listdir(modelpath)
x = 'x'
y = 'y'
def HelloWidget(QWidget):
def __init__(self):
for file in filelist:
button = QPushButton(file)
button.clicked.connect(lambda x=x,y=y,file=file: self.myfunction(x,y,file)
def myfunction(self,x,y,file):
print(x)
print(y)
print(file)
问题的产生是因为clicked http://doc.qt.io/qt-5/qabstractbutton.html#clicked传递一个布尔值,指示是否已被检查。适当的事情是使用参数来使用该参数:
button.clicked.connect(lambda checked, x=x,y=y,file=file: self.myfunction(x,y,file))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)