從環境(它是一個名爲nuke的圖形程序)給出了一個全局對象,我可以添加菜單項並將它與函數連接。這個全局對象是這樣工作的:如何結合getattr()動態地使用lambda與python模塊相結合:在python中?
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
動態導入工作得很好。但是每個生成的菜單項都鏈接到上次導入的函數。所以每一個菜單項都是一樣的。
我不是專業程序員。所以我認爲我犯了一個簡單的錯誤。
感謝您的幫助。
謝謝。這樣可行!!!我在那失去了半天。 – Jonas 2012-07-10 20:41:59
@Jonas看看我的答案中提供的解釋。 – Marcin 2012-07-10 20:46:31
@Jonas隨時。請確保將回答您的問題的答案標記爲「已回答」(旁邊的複選標記)。這會將您的問題標記爲已回答。 – Lanaru 2012-07-10 20:49:11