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!!

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to