当前位置:网站首页>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
边栏推荐
猜你喜欢
自动机器学习框架介绍与使用(flaml、h2o)
MySQL主從複制、讀寫分離
CSDN markdown editor
Software I2C based on Hal Library
error C4996: ‘strcpy‘: This function or variable may be unsafe. Consider using strcpy_s instead
Swagger, Yapi interface management service_ SE
QT creator custom build process
Dotnet replaces asp Net core's underlying communication is the IPC Library of named pipes
保姆级出题教程
AcWing 1298.曹冲养猪 题解
随机推荐
QT creator create button
[Thesis Writing] how to write function description of jsp online examination system
Tcp/ip protocol (UDP)
Install mysql5.5 and mysql8.0 under windows at the same time
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
Did you forget to register or load this tag
Cookie setting three-day secret free login (run tutorial)
[number theory] divisor
AcWing 1294. Cherry Blossom explanation
MTCNN人脸检测
Software I2C based on Hal Library
Swagger, Yapi interface management service_ SE
AcWing 242. A simple integer problem (tree array + difference)
[蓝桥杯2021初赛] 砝码称重
Ansible practical Series III_ Task common commands
AcWing 1294.樱花 题解
安全测试涉及的测试对象
How to set up voice recognition on the computer with shortcut keys
Detailed reading of stereo r-cnn paper -- Experiment: detailed explanation and result analysis
The virtual machine Ping is connected to the host, and the host Ping is not connected to the virtual machine