Help!!! On Tkinter Menu problem!!!

X

Xuening

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

Steve Holden

Xuening said:
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.
[code snipped]


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!!
Well first of all I presume you understand that menus have a delete
method, so you can remove items. If not, see "delete()" in

http://effbot.org/tkinterbook/menu.htm

Are these top-level windows you are deleting? If so, look under
"Protocol" in

http://effbot.org/zone/tkinter-events-and-bindings.htm

for details of how to handle WM_DELETE_WINDOW. The handler for each
window can delete the appropriate menu item.

regards
Steve
 
M

Martin Franklin

Xuening said:
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!!



The Menu Tk Widget has a delete or deletecommand method(s) the first
accepts an index the second a Menu label, both will remove the menu entry

for more on this...


http://effbot.org/tkinterbook/menu.htm

Regards
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top