xml menu pygtk
Seb
sebastianthegreatful at gmail.com
Sat May 16 11:15:24 EDT 2009
On May 16, 5:04 pm, MRAB <goo... at mrabarnett.plus.com> wrote:
> Seb wrote:
> > On May 16, 4:20 pm, MRAB <goo... at mrabarnett.plus.com> wrote:
> >> Seb wrote:
> >>> I'm trying to construct a menu from an xml file. However my recursive
> >>> algorithm isn't doing what I want it too. I've been starring at this
> >>> for too long. Any help appreciated :)
> >>> I get the following error but the problem is more of a logical nature.
> >>> ./gnomeAppletMenu.py:40: GtkWarning: gtk_menu_shell_insert: assertion
> >>> `GTK_IS_MENU_ITEM (child)' failed
> >>> menu_bar.append(menu)
> >>> I'm pretty sure I fuck up around line 27-30
> >>> xml file:
> >>>http://pastie.org/480045
> >>> python code:
> >>>http://pastie.org/480042
> >> The traceback suggests to me that you can add only a 'MenuItem' to a
> >> 'MenuBar', but create_menu() is returning a 'Menu'.
>
> > Doesn't a MenuBar consists of Menu's?
>
> I've had a look at some examples on the web and it looks like you need
> to:
>
> 1. Set the menu on a menu item (the 'set_submenu' method);
> 2. Append the menu item onto the menu bar.
>
> What you're actually doing is:
>
> 1. Setting the menu on a menu item;
> 2. Appending the menu item onto a menu;
> 3. Trying to append the menu onto the menu bar.
>
> Result: exception.
>
> HTH
Ok, now it looks like below. I don't get any errors but the dictionary
entry show up as a toplevel menu... damn!!
def create_menu(node):
menus = []
for child in node.childNodes:
if child.localName == "item":
menus.append(gtk.MenuItem(child.getAttribute("name")))
if child.localName == "seperator":
menus.append(gtk.SeparatorMenuItem())
if child.localName == "menu": #if child.childNodes:
menuitem = gtk.MenuItem(child.getAttribute("name"))
menu = gtk.Menu()
for mi in create_menu(child): # for each menuitem
menu.append(mi) # append each menuitem to menu
menuitem.set_submenu(menu) # set menu as submenu of menuitem
menus.append(menuitem)
return menus
def factory(applet, iid):
doc = minidom.parse("menu.xml")
rootNode = doc.documentElement
menu_bar = gtk.MenuBar()
for cn in rootNode.childNodes: # for each menu under menus
for menu in create_menu(cn): # for each menu in list
menu_bar.append(menu) # append each menu
applet.add(menu_bar)
applet.show_all()
return True
More information about the Python-list
mailing list