Help!!! On Tkinter Menu problem!!!



I have a problem about menu by using Tkinter. I want to make a dynamic
menu. That is when I create a new view/window, one menuitem will be
added to the menu. and when I kill a window, the correponding menuitem
will be deleted. I write a simple code and I can add menuitem now. But
I don't know how to remove the item when I close the window.

The following is my code:


==============================
from Tkinter import*

class App:
def __init__(self):
root=Tk()
self.root=root
self.doMenus()
root.config(menu=self.mBar)
delete=deletewindow()
self.root.mainloop()

def doMenus(self):
mBar=Menu(self.root)
self.mBar=mBar
self.doFileMenu()
self.doWindowMenu()

def doFileMenu(self):
filemenu = Menu(self.mBar,tearoff=0,background='yellow')
filemenu.add_command(label='New
window',underline=0,command=self.newWindow)
filemenu.add('separator')

filemenu.add_command(label='Quit',underline=0,command=self.root.destroy)
self.filemenu=filemenu
self.mBar.add_cascade(label="File", menu=self.filemenu)

def doWindowMenu(self):
WinName = Menu(self.mBar,
tearoff=0,postcommand=self.windowListUpdate)
self.windowMenu=WinName
self.mBar.add_cascade(label="Window", menu=self.windowMenu)
self.windowList=[]
self.nWindows=0

def windowListUpdate(self):
self.windowMenu.delete(0, END)
for window in self.windowList:
self.windowMenu.add_command(label=window)


def newWindow(self):
self.nWindows+=1
windowName = "Window %d"% self.nWindows
self.windowList.append(windowName)
self.windowListUpdate()

root2=Tk()
self.root2=root2
self.root2.title(windowName)
canvas=Canvas(self.root2, width=450,height=300,bg='green')
canvas.pack()
self.canvas=canvas

if __name__ == '__main__':
test=App()


Maybe I should bind something to <destroy> event. Everytime I close the
window, the name of the window should be removed from the windowList
and refresh the all the menuitems again. But I don't know how and
where to add the "destroy" event. Can anyone give me a hand????

Thanks a lot!!

.



Relevant Pages

  • Having problems using Tkinter
    ... application using Tkinter in which a new window pops out on a ... from Tkinter import * ... def buttons: ... #app = AddKlas ...
    (comp.lang.python)
  • tkFileDialog closes main application
    ... So I have decided to use tkFileDialog askopenfilename. ... I've just started using tkinter so I have no idea what I may be doing wrong. ... Do this repeatedly and for me after the sixth or seventh time the window shuts down. ... def browse: ...
    (comp.lang.python)
  • Tkinter, resize window, keep widgets relative placements?
    ... but i tried with pack and grid before and had trouble making it ... is it possible to have the minimized window just being placed in the ... from Tkinter import * ... def Disp: ...
    (comp.lang.python)
  • message entry box at center
    ... from Tkinter import * ... def callback: ... i want to show the entry button at the center of the window. ...
    (comp.lang.python)
  • Re: refreshing a ruby tk window/scrollbox
    ... new window with the sorting option. ... def X11ColorCategory.read_rgb_table ... # label displaying selected color. ...
    (comp.lang.ruby)