PyQt5 按钮 lambda 变量变为布尔值 [英] PyQt5 button lambda variable becomes boolean

查看:20
本文介绍了PyQt5 按钮 lambda 变量变为布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行下面的代码时,它显示如下.为什么不是 x 'x' 而是变成布尔值?这只发生在传递给使用 lambda 调用的函数的第一个参数.

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.

假是/home/me/model/some_file

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)

推荐答案

问题是因为clicked 传递一个布尔值,指示它是否已被选中.适当的做法是使用参数来使用该参数:

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

这篇关于PyQt5 按钮 lambda 变量变为布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆