当前位置:网站首页>使用lambda在循环中传参时,参数总为同一个值
使用lambda在循环中传参时,参数总为同一个值
2022-07-06 09:14:00 【imxlw00】
当我使用PyQt5时,需要在调用事件时传入参数,于是想到了lambda函数,代码如下:
for i, button in enumerate(self.button_dict.values()):
button.clicked.connect(lambda: self.click_checkbox(i))
此时i都是同一个值。
lambda函数(或称闭包)传递的不是参数的值,而是参数的物理地址,所以lambda传递的参数都会是同一个值。想要解决可以使用functools模块。
解决方案
导入functools模块,使用partial方法。更改的代码如下:
button.clicked.connect(partial(self.click_checkbox, i))
参考:https://blog.csdn.net/qq_34965596/article/details/100579458
边栏推荐
- [recommended by bloggers] asp Net WebService background data API JSON (with source code)
- Ansible practical Series III_ Task common commands
- error C4996: ‘strcpy‘: This function or variable may be unsafe. Consider using strcpy_s instead
- Did you forget to register or load this tag
- Dotnet replaces asp Net core's underlying communication is the IPC Library of named pipes
- Attention apply personal understanding to images
- QT creator uses Valgrind code analysis tool
- Learning question 1:127.0.0.1 refused our visit
- MySQL completely uninstalled (windows, MAC, Linux)
- Image recognition - pyteseract TesseractNotFoundError: tesseract is not installed or it‘s not in your path
猜你喜欢
随机推荐
Knowledge Q & A based on Apache Jena
Are you monitored by the company for sending resumes and logging in to job search websites? Deeply convinced that the product of "behavior awareness system ba" has not been retrieved on the official w
AcWing 242. A simple integer problem (tree array + difference)
Ansible practical Series III_ Task common commands
02-项目实战之后台员工信息管理
Codeforces Round #753 (Div. 3)
Postman uses scripts to modify the values of environment variables
【博主推荐】C#生成好看的二维码(附源码)
Navicat 導出錶生成PDM文件
When you open the browser, you will also open mango TV, Tiktok and other websites outside the home page
Base de données Advanced Learning Notes - - SQL statements
【博主推荐】C# Winform定时发送邮箱(附源码)
Deoldify项目问题——OMP:Error#15:Initializing libiomp5md.dll,but found libiomp5md.dll already initialized.
AcWing 1294.樱花 题解
[recommended by bloggers] background management system of SSM framework (with source code)
AcWing 179.阶乘分解 题解
CSDN markdown editor
Copie maître - esclave MySQL, séparation lecture - écriture
解决安装Failed building wheel for pillow
L2-007 家庭房产 (25 分)