我在学习 Python 的同时制作了一个简单的小实用程序。它动态生成按钮列表:
for method in methods:
button = Button(self.methodFrame, text=method, command=self.populateMethod)
button.pack({'fill': 'x', 'expand': 1, 'padx': 5, 'pady': 3})
那部分工作正常。但是,我需要知道里面按下了哪个按钮self.populateMethod
。关于我如何辨别有什么建议吗?
您可以使用 lambda 将参数传递给命令:
def populateMethod(self, method):
print "method:", method
for method in ["one","two","three"]:
button = Button(self.methodFrame, text=method,
command=lambda m=method: self.populateMethod(m))
button.pack({'fill': 'x', 'expand': 1, 'padx': 5, 'pady': 3})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)