python - 带有可变类命令的 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()))
这可能吗?
解决方案
推荐阅读
- angular - 如果字符超出 textarea 中的特定限制,则更改字符颜色
- modbus - 如何连接opc服务器?
- asp.net-mvc-5 - 如果数据计数超过 10K,free-jQgrid 会抛出 SystemOutofMemoryException
- ruby - 无法安装 raakt/hpricot gem
- algorithm - 从多个选项算法中为一组分配一些值的问题
- javascript - 获取通向 D3 视觉中被点击元素的路径 (flare.json)
- java - SonarQube 忽略 @SuppressWarnings("all") 命令
- material-ui - React,Material-UI,网格空项目
- javascript - 设置要下降的列的基值
- c++ - 比较更多 50 个字符串的有效方法