【问题标题】:how to define a series of buttons and run a function with a different input for each of them tkinter python如何定义一系列按钮并为每个按钮运行一个具有不同输入的函数 tkinter python
【发布时间】:2019-07-28 23:15:19
【问题描述】:

所以我使用一个for循环来定义一系列按钮:

buy = [None] * 11
for x in range(11):
    buy[x] = Button(buyf, text = ProjectList[x][2],command= lambda: buycheck(x), bg = "red", fg = "white")

为什么当我按下任何按钮时,它们只返回最大值(11)? 我希望每个按钮在分配所述按钮时通过输入 x 值来调用函数 buycheck。此外,按钮被放入预先确定大小的数组中,然后打开和关闭引用。

【问题讨论】:

    标签: python for-loop button tkinter


    【解决方案1】:

    这是因为buycheck(x) 将始终与x = 10(for 循环后的值)一起执行。您应该将 x 作为 lambda 的第一个参数的默认值传递,如下所示:

    command=lambda n=x: buycheck(n)
    

    【讨论】:

      【解决方案2】:

      看看Generate Tkinter buttons dynamically,根据你说的(不完全清楚)你在使用动态按钮时遇到了一些问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-13
        • 2015-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多