PyQt5 button lambda variable becomes boolean [dupl

2020-01-30 02:37发布

When I run the code below, it shows the below. Why isn't x 'x' but becomes a boolean? This happens only to the first argument passed into the function called with lambda.

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

1条回答
2楼-- · 2020-01-30 02:56

The problem is caused because clicked passes a Boolean value indicating whether it has been checked or not. the appropriate thing is to use a parameter to use that argument:

button.clicked.connect(lambda checked, x=x,y=y,file=file: self.myfunction(x,y,file))
查看更多
登录 后发表回答