1

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)
user1657862
  • 83
  • 1
  • 8

1 Answers1

5

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))
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • I suppose it is correct. I just found the answer here as well. https://stackoverflow.com/questions/18836291/lambda-function-returning-false. Thanks for your quick reply! – user1657862 Nov 18 '17 at 16:52