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

1 Answer 1

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))
Sign up to request clarification or add additional context in comments.

1 Comment

I suppose it is correct. I just found the answer here as well. stackoverflow.com/questions/18836291/…. Thanks for your quick reply!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.