鱼C论坛

 找回密码
 立即注册
查看: 2301|回复: 5

[已解决]tkinter怎么for循环创建多个参数不同的Button

[复制链接]
发表于 2021-2-4 21:11:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 lawrence1357 于 2021-2-4 21:27 编辑

from tkinter import *

class For_button:
    def __init__(self, root):
        self.root = root
        for x  in range(10) :
            locals()['bnt'+str(x)]=Button(self.root, text=x)
            locals()['bnt'+str(x)].grid()
            locals()['bnt'+str(x)].config(command = lambda: self.fun_with_args(x))
            

    def fun_with_args(self,x):
        num=x
        print('------------2-------------')
        print(num)


if __name__=='__main__':
    root=Tk()
    For_button(root)

    mainloop()


这个参数x每一次都传的是9,不应该是第一个button传0,第二个传1吗
最佳答案
2021-2-4 21:23:22
  1. class For_button:
  2.     def __init__(self, root):
  3.         self.root = root
  4.         for x in range(10) :
  5.             locals()['bnt'+str(x)]=Button(self.root, text=x)
  6.             locals()['bnt'+str(x)].grid()
  7.             locals()['bnt'+str(x)].config(command = self.fun_with_args(x))
  8.             

  9.     def fun_with_args(self,x):
  10.         def r():
  11.             num=x
  12.             print('------------2-------------')
  13.             print(num)
  14.         return r
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-2-4 21:23:22 | 显示全部楼层    本楼为最佳答案   
  1. class For_button:
  2.     def __init__(self, root):
  3.         self.root = root
  4.         for x in range(10) :
  5.             locals()['bnt'+str(x)]=Button(self.root, text=x)
  6.             locals()['bnt'+str(x)].grid()
  7.             locals()['bnt'+str(x)].config(command = self.fun_with_args(x))
  8.             

  9.     def fun_with_args(self,x):
  10.         def r():
  11.             num=x
  12.             print('------------2-------------')
  13.             print(num)
  14.         return r
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2021-2-4 21:23:44 | 显示全部楼层

  1. from tkinter import *

  2. class For_button:
  3.     def __init__(self, root):
  4.         self.root = root
  5.         for x  in range(10) :
  6.             locals()['bnt'+str(x)]=Button(self.root, text=x)
  7.             locals()['bnt'+str(x)].grid()
  8.             locals()['bnt'+str(x)].config(command = lambda x=x: self.fun_with_args(x))
  9.             

  10.     def fun_with_args(self,x):
  11.         num=x
  12.         print('------------2-------------')
  13.         print(num)


  14. if __name__=='__main__':
  15.     root=Tk()
  16.     For_button(root)

  17.     mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-4 21:30:36 | 显示全部楼层
  1. from functools import partial
  2.             locals()['bnt'+str(x)].config(command = partial(self.fun_with_args,x))
复制代码

还想起来个方法
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-2-5 23:56:36 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-2-5 23:57:58 | 显示全部楼层

我又遇到一个问题,发了一个求助帖,如果可以的话请您帮忙解答一下,谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-28 03:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表