当前位置:网站首页>When using lambda to pass parameters in a loop, the parameters are always the same value
When using lambda to pass parameters in a loop, the parameters are always the same value
2022-07-06 11:24:00 【imxlw00】
When I use PyQt5 when , You need to pass in parameters when calling events , So I thought of lambda function , The code is as follows :
for i, button in enumerate(self.button_dict.values()):
button.clicked.connect(lambda: self.click_checkbox(i))
here i It's all the same value .
lambda function ( Or closure ) What is passed is not the value of the parameter , It is the physical address of the parameter , therefore lambda The parameters passed will all be the same value . If you want to solve it, you can use functools modular .
Solution
Import functools modular , Use partial Method . The changed code is as follows :
button.clicked.connect(partial(self.click_checkbox, i))
Reference resources :https://blog.csdn.net/qq_34965596/article/details/100579458
边栏推荐
- 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
- 基于apache-jena的知识问答
- [ahoi2009]chess Chinese chess - combination number optimization shape pressure DP
- Introduction to the easy copy module
- Redis的基础使用
- Aborted connection 1055898 to db:
- Swagger, Yapi interface management service_ SE
- 报错解决 —— io.UnsupportedOperation: can‘t do nonzero end-relative seeks
- AcWing 1298.曹冲养猪 题解
- 引入了junit为什么还是用不了@Test注解
猜你喜欢
Neo4j installation tutorial
Case analysis of data inconsistency caused by Pt OSC table change
Basic use of redis
Did you forget to register or load this tag
Software testing and quality learning notes 3 -- white box testing
In the era of DFI dividends, can TGP become a new benchmark for future DFI?
[ahoi2009]chess Chinese chess - combination number optimization shape pressure DP
自动机器学习框架介绍与使用(flaml、h2o)
Django running error: error loading mysqldb module solution
vs2019 使用向导生成一个MFC应用程序
随机推荐
Learn winpwn (3) -- sEH from scratch
01项目需求分析 (点餐系统)
记某公司面试算法题:查找一个有序数组某个数字出现的次数
Knowledge Q & A based on Apache Jena
What does BSP mean
MySQL completely uninstalled (windows, MAC, Linux)
Ansible实战系列一 _ 入门
UDS learning notes on fault codes (0x19 and 0x14 services)
Rhcsa certification exam exercise (configured on the first host)
PyCharm中无法调用numpy,报错ModuleNotFoundError: No module named ‘numpy‘
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
人脸识别 face_recognition
L2-007 家庭房产 (25 分)
Machine learning -- census data analysis
[蓝桥杯2020初赛] 平面切分
QT creator support platform
Principes JDBC
连接MySQL数据库出现错误:2059 - authentication plugin ‘caching_sha2_password‘的解决方法
Unable to call numpy in pycharm, with an error modulenotfounderror: no module named 'numpy‘
Remember the interview algorithm of a company: find the number of times a number appears in an ordered array