tkinter menuitem question
Matthew Dixon Cowles
matt at mondoinfo.com
Wed Mar 20 13:50:37 EST 2002
On 20 Mar 2002 14:24:33 +0000, Ken Guest <kwg at renre-europe.com> wrote:
Dear Ken,
>ok, I might be way off here but is there a way with a collection of
>Tkinter menuitems that all have their callbacks set to be the one
>function to determine which menuitem was clicked on?
Yes, you can do that by storing the state in an object that has
a __call__ method. I'll append a small example.
Regards,
Matt
from Tkinter import *
class genericCBO:
def __init__(self,func,*positionArgs,**kwArgs):
self.func=func
self.positionArgs=positionArgs
self.kwArgs=kwArgs
return None
def __call__(self):
return apply(self.func,self.positionArgs,self.kwArgs)
return None
class mainWin:
def __init__(self,root):
self.root=root
self.createWidgets()
return None
def createWidgets(self):
menubar=Menu(self.root)
myMenu=Menu(menubar)
myMenu.add_command(label="Option 1",command=genericCBO(self.menuCB,1))
myMenu.add_command(label="Option 2",command=genericCBO(self.menuCB,2))
menubar.add_cascade(label="Menu",menu=myMenu)
self.root.config(menu=menubar)
return None
def menuCB(self,index):
print index
return None
def main():
root=Tk()
mainWin(root)
root.mainloop()
return None
if __name__=='__main__':
main()
More information about the Python-list
mailing list