从环境(它是一个名为nuke的图形程序)被赋予一个全局对象,我可以在其中添加菜单项并将其与函数连接起来。这个全局对象是这样工作的:
menu.addCommand("Do This!", lambda: DoThings())我想动态读取python模块,并将模块函数添加为全局菜单对象中的项目。我写了一个课程来做到这一点。我的课程的简化版本:
class mymenu():
.
.
.
def _builMenuFromPath(self, basepath, module):
items = []
# ...
# there is code to build the items list. this is a list of the python filenames
# ...
if len(items) > 0:
for item in items:
try:
f = getattr(__import__(module + "." + item), item) # item is the python filename of an module in the folder with the python files and module is the folder
menu.addCommand(item, lambda: f.call()) # f.call() is a function in the dynamic loaded python file
except Exception, e:
pass动态导入效果很好。但是每个生成的菜单项都链接到上次导入的函数。所以每一个菜单项都是一样的。
我不是一个专业的程序员。所以我认为我犯了一个简单的错误。
谢谢你的帮助。