当前位置:网站首页>使用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
- Remember a company interview question: merge ordered arrays
- [number theory] divisor
- L2-007 家庭房产 (25 分)
- double转int精度丢失问题
- What does usart1 mean
- Principes JDBC
- Software testing and quality learning notes 3 -- white box testing
- 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
- @Controller, @service, @repository, @component differences
猜你喜欢

Copie maître - esclave MySQL, séparation lecture - écriture

How to build a new project for keil5mdk (with super detailed drawings)

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

引入了junit为什么还是用不了@Test注解

QT creator custom build process

Leetcode 461 Hamming distance

MySQL主從複制、讀寫分離

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

Swagger、Yapi接口管理服务_SE

【博主推荐】SSM框架的后台管理系统(附源码)
随机推荐
数数字游戏
Basic use of redis
Invalid default value for 'create appears when importing SQL_ Time 'error reporting solution
Leetcode 461 Hamming distance
The virtual machine Ping is connected to the host, and the host Ping is not connected to the virtual machine
引入了junit为什么还是用不了@Test注解
Introduction and use of automatic machine learning framework (flaml, H2O)
Number game
Solution: log4j:warn please initialize the log4j system properly
Ansible实战系列一 _ 入门
NPM an error NPM err code enoent NPM err syscall open
Punctual atom stm32f103zet6 download serial port pin
Principes JDBC
[蓝桥杯2017初赛]包子凑数
MySQL的一些随笔记录
TCP/IP协议(UDP)
Julia 1.6 1.7 common problem solving
01 project demand analysis (ordering system)
安全测试涉及的测试对象
Generate PDM file from Navicat export table