views:

278

answers:

1

Hello. I am trying to write an extension for nautilus, which add an item to the menu that appears when you right-click a file (as shown in image)

alt text

However, I would like to add a submenu to my custom menu item.

I downloaded a 'nautilus-python' package which includes examples of how to write extensions for Nautilus (and so far it turned out to be the best/only documentation i found). In it, is a file called submenu.py, which contains the following:

import nautilus

class ExampleMenuProvider(nautilus.MenuProvider):

    def get_file_items(self, window, files):
        menuitem = nautilus.MenuItem('ExampleMenuProvider::Foo', 'Foo', '')

        submenu = nautilus.Menu()
        menuitem.set_submenu(submenu)

        menuitem = nautilus.MenuItem('ExampleMenuProvider::Bar','Bar','')
        submenu.append_item(menuitem)

        return menuitem,

    # FIXME: Why isn't this working?
    def get_background_items(self, window, file):
        submenu = nautilus.Menu()
        submenu.append_item(nautilus.MenuItem('ExampleMenuProvider::Bar', 'Bar', ''))

        menuitem = nautilus.MenuItem('ExampleMenuProvider::Foo', 'Foo', '')
        menuitem.set_submenu(submenu)

        return menuitem,

ps: i didn't add "# FIX ME: Why isnt this working?". that is actually included in the example

The code doesn't work. Even if i take out the second function and leave just the first one, it doesn't work.

Any help would be appreciated, thanks.

A: 

I found the solution.

  1. you need a init function declared.
  2. had to make some variable name change to the example above

    import nautilus
    
    
    class ExampleMenuProvider(nautilus.MenuProvider):
    
    
    
    def __init__(self):
     pass
    
    
    def get_file_items(self, window, files):
    
    
     item = nautilus.MenuItem('Nautilus::sbi','Nau-T','image')
     submenu = nautilus.Menu()
    
    
     item.set_submenu(submenu)
    
    
     item_two = nautilus.MenuItem('Nautilus::s','www','image')
    
    
     submenu.append_item(item_two)
    
    
     return item,
    
lyrae