3

I am trying to implement a list of recently opened items in the menu of a GUI I am writing with PyGTK. I initialize the menu like so:

self.filemenu = gtk.Menu()

self.init_file_menu()

self.fileitem = gtk.MenuItem("File")
self.fileitem.set_submenu(self.filemenu)
menubar = gtk.MenuBar()
menubar.append(self.fileitem)
outerframe.pack_start(menubar, False, False)

def init_file_menu(self):
    for widget in self.filemenu.get_children():
        self.filemenu.remove(widget)

    openitem = gtk.MenuItem("Open")
    self.filemenu.append(openitem)
    openitem.connect("activate", self.open_file)

    self.filemenu.append(gtk.SeparatorMenuItem())

    for recentitem in self.settings['recentfiles']:
        item = gtk.MenuItem(os.path.basename(recentitem))
        self.filemenu.append(item)
        item.connect("activate", self.open_file, recentitem)
    self.filemenu.show()

self.settings['recentitems'] is a deque that is modified when opening a file; init_file_menu is then called again. My goal is to empty the menu, then repopulate it with the right items in the right order. This code works fine when populating the menu at startup. But for some reason while the calls to Menu.remove all work fine and empty the menu, the append calls do not re-add the items to it and it remains empty. If I call its get_children method, I see that they are all there internally, but the interface has not been updated to reflect this. How do I refresh the displayed menu and make the recent items list work as expected?

I would also accept direction on how to use the RecentChooserMenu widget if that is what I am looking for.

1 Answer 1

5

Since gtk.Menu is a container, you need to .show every item after adding it to menu (Because you don't want to call menu.show_all() shortcut which shows the menu itself), so you do item.show() just after menu.append(item)

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, right, that makes more sense than what I was trying. self.filemenu.show_all() also appears to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.