PyQt5 按钮 lambda 变量变为布尔值
PyQt5 button lambda variable becomes boolean
当我运行下面的代码时,它显示如下。为什么 x 'x' 不是而是变成布尔值?这只会发生在传递给用 lambda 调用的函数的第一个参数上。
错误
是
/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
传递了一个布尔值表示是否被检查过。适当的事情是使用参数来使用该参数:
button.clicked.connect(lambda checked, x=x,y=y,file=file: self.myfunction(x,y,file))
当我运行下面的代码时,它显示如下。为什么 x 'x' 不是而是变成布尔值?这只会发生在传递给用 lambda 调用的函数的第一个参数上。
错误 是 /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
传递了一个布尔值表示是否被检查过。适当的事情是使用参数来使用该参数:
button.clicked.connect(lambda checked, x=x,y=y,file=file: self.myfunction(x,y,file))