首页 > 解决方案 > 带有可变类命令的 Python Tkinter 按钮

问题描述

抱歉,如果标题令人困惑,我真的不知道如何描述我在这里尝试做的事情。我想使用 lambda 函数来获取类方法。基本上我想这样做

def listButton(frame, ls, command, arg):
    for item in ls:
        // For each item in the list, add a button whose command is a class method of the type of item,
        // the argument of which is another class method of the type of item.
        frame.addButton(item.display(), lambda: item.command(item.arg))
    frame.update()

我收到一个关于项目类型没有属性“command”的错误,这是正确的,但我想将我的参数“command”替换为 item.command。

如果这令人困惑,我将提供一个用例:

输入

listButton(frame, listOfItems, lambda arg: print(arg), lambda: display())

我希望如何解释有问题的行

// For the first iteration of the for loop, for example
frame.addButton(frame, lambda: listOfItems[0].print(listOfItems[0].display()))

这可能吗?

标签: pythontkinterlambda

解决方案


推荐阅读