python 批量添加的button 使用同一点击事件的方法

作者:Cod_erL 时间:2022-10-11 20:13:46 

python 批量添加的button 使用同一点击事件根据传递的参数进行区分。


def clear_text():

print '我只是个清空而已'
def clear_text(index):

print '我只是个清空而已' +str(index)
button = Button(framet_title, text='清空', command=clear_text)

这样去设置,单个按钮对应单个点击事件没有问题的

如果你是


for i in Range(10):

button = Button(framet_title, text='清空', command=clear_text_list(i))

这样搞的话 当你程序启动的时候 回调函数就会直接执行,点击按钮是没有任何反应的,使用 button.bind的方式几乎是一样的

这种写法 如果是在JAVA C HTML C++ 是没有任何问题的。请原谅 我是个做android开发 程序员。我最纳闷的是 我明明 是每个按钮都给他单独设置了 回调。为什么会提前回调,并且 点击的时候没有任何反应。


for i in Range(10):

button = Button(framet_title, text='清空', command=lambda:clear_text_list(i))

如果在 添加 lambda 程序启动是不会回调了。但是每次点击 获得的索引还是最后一个。

Google 了一圈 (说实话 我不知道用什么关键字。。。) http://stackoverflow.com/ 上面我尝试 python buttons command lambda

找到了 http://stackoverflow.com/questions/20596892/disabling-buttons-after-click-in-tkinter

正确的姿势是


for index in range(9):
n=letters[index]

button = Button(root, bg="White", text=n, width=5, height=1, relief=GROOVE,
    command=lambda index=index, n=n: appear(index, n))

看到答案 之后 开始找答案对的理由吧 他是用lambda 表达式 做成了个 点击事件的集合 正好对应 button的集合。

来源:https://blog.csdn.net/u014510302/article/details/52535371

标签:python,批量,添加,button
0
投稿

猜你喜欢

  • [JS]用 或 || 来兼容FireFox

    2013-06-26 14:50:47
  • ubuntu20.04配置mysql8.0的实现步骤

    2024-01-28 14:56:16
  • python制作机器人的实现方法

    2022-11-09 21:09:05
  • php去除html标记的原生函数详解

    2024-03-17 20:23:22
  • 使用Django框架创建项目

    2023-02-14 10:30:25
  • SQL Server误区30日谈 第29天 有关堆碎片的误区

    2024-01-20 06:20:06
  • Python如何使用k-means方法将列表中相似的句子归类

    2022-11-01 21:16:32
  • python使用requests.session模拟登录

    2022-09-12 19:17:24
  • Mysql安装与配置调优及修改root密码的方法

    2024-01-15 19:35:06
  • ASP中的Debug类--VBScript

    2008-10-24 09:38:00
  • python安装教程 Pycharm安装详细教程

    2022-08-08 02:25:55
  • 微信公众平台网页授权获取用户基本信息中授权回调域名设置的变动

    2023-11-14 14:01:31
  • 纯CSS Tooltips提示

    2008-10-18 16:01:00
  • Utf-8和Gb2312乱码问题的终结

    2008-04-05 14:04:00
  • 交互设计模式(三)-Tagging(标签)

    2009-10-19 20:46:00
  • Python Tornado核心及相关原理详解

    2023-04-21 23:29:40
  • 全兼容可高亮二级缓冲折叠菜单

    2010-06-03 16:53:00
  • 基于Python绘制520表白代码

    2021-06-10 08:14:44
  • php注册登录系统简化版

    2024-04-30 08:48:24
  • 解决oracle用户连接失败的解决方法

    2011-01-04 19:35:00
  • asp之家 网络编程 m.aspxhome.com