gpt4 book ai didi

python - 如何将字符串转换为命令

转载 作者:太空宇宙 更新时间:2023-11-04 10:29:45 24 4
gpt4 key购买 nike

我想将字典中的字符串转换为可调用的命令。

binds = {"<Control-n>" : 'Commands.New()'}
for n, b in binds.values():
root.bind_all(n, b)

所以我想绑定(bind)字典中的所有内容。

最佳答案

不要将命令存储为字符串。确实没有一种干净的方法可以将它们转换回可以与 bind_all 一起使用的函数。

相反,您应该使字典的值成为对函数对象本身的引用:

binds = {"<Control-n>" : Commands.New}
for n, b in binds.items():
root.bind_all(n, b)

此外,您应该调用 dict.items(或 Python 2.x 中的 dict.iteritems)而不是 dict.values。第一种方法将返回键/值对的元组,您可以在执行操作时将其解包。然而,第二种方法只返回值。

关于python - 如何将字符串转换为命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27552116/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com