当前位置:网站首页>使用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
边栏推荐
- 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
- QT creator specify editor settings
- JDBC原理
- Swagger, Yapi interface management service_ SE
- Deoldify project problem - omp:error 15:initializing libiomp5md dll,but found libiomp5md. dll already initialized.
- When you open the browser, you will also open mango TV, Tiktok and other websites outside the home page
- [recommended by bloggers] C # generate a good-looking QR code (with source code)
- 学习问题1:127.0.0.1拒绝了我们的访问
- Ansible实战系列二 _ Playbook入门
- AI benchmark V5 ranking
猜你喜欢

【博主推荐】C#生成好看的二维码(附源码)

【博主推荐】C#MVC列表实现增删改查导入导出曲线功能(附源码)

打开浏览器的同时会在主页外同时打开芒果TV,抖音等网站

学习问题1:127.0.0.1拒绝了我们的访问

Idea import / export settings file

Did you forget to register or load this tag 报错解决方法

02-项目实战之后台员工信息管理

When you open the browser, you will also open mango TV, Tiktok and other websites outside the home page

自动机器学习框架介绍与使用(flaml、h2o)

1. Mx6u learning notes (VII): bare metal development (4) -- master frequency and clock configuration
随机推荐
记一次某公司面试题:合并有序数组
MySQL主從複制、讀寫分離
Project practice - background employee information management (add, delete, modify, check, login and exit)
安全测试涉及的测试对象
自动机器学习框架介绍与使用(flaml、h2o)
软件测试-面试题分享
MySQL other hosts cannot connect to the local database
Ubuntu 20.04 安装 MySQL
Picture coloring project - deoldify
Introduction to the easy copy module
Unable to call numpy in pycharm, with an error modulenotfounderror: no module named 'numpy‘
误删Path变量解决
Tcp/ip protocol (UDP)
L2-007 家庭房产 (25 分)
Error reporting solution - io UnsupportedOperation: can‘t do nonzero end-relative seeks
What does BSP mean
L2-006 树的遍历 (25 分)
Asp access Shaoxing tourism graduation design website
[ahoi2009]chess Chinese chess - combination number optimization shape pressure DP
What does usart1 mean