PyQt5 button lambda variable becomes boolean [duplicate]

喜你入骨 提交于 2019-11-26 18:44:03

问题


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:


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


来源:https://stackoverflow.com/questions/47368468/pyqt5-button-lambda-variable-becomes-boolean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!