How do I insert a menu item in an existing menu.

Discussion in 'Python' started by BlueFlash, Aug 26, 2009.

  1. BlueFlash

    BlueFlash Guest

    Idea: Use of a RecentFileList, update element 0 with new file opened.

    Appending a menu is easy
    menu.Append(-1, "Menu Name")

    when I try to insert a new menu item
    menu.Insert(-1, position, "Menu Name")

    does not work.

    full code:
    def addnew(self, position, path):
    path = path.strip()
    try:
    self.filelist.index(path) # index() throws ValueError
    if path does not exist.
    except:
    # ValueError, path is not in list, so add it:
    if position >= len(self.filelist):
    self.filelist.append(path)
    item = self.menu.Append(-1, path) #this does return a
    menu item
    self.window.Bind(wx.EVT_MENU, self.event, item)
    else:
    self.filelist.insert(position, path)
    item = self.menu.InsertItem(-1, position, path) # does
    Not return a menu item. throws some exception.
    print item
    #self.window.Bind(wx.EVT_MENU, self.event, item)


    How do I remove a menu item from a menu? The RecentUsedList is eg: 4
    element long. When opening a new file, this should be added at the
    top, and the 4th element must be dropped.
    BlueFlash, Aug 26, 2009
    #1
    1. Advertising

  2. BlueFlash

    MRAB Guest

    BlueFlash wrote:
    > Idea: Use of a RecentFileList, update element 0 with new file opened.
    >
    > Appending a menu is easy
    > menu.Append(-1, "Menu Name")
    >
    > when I try to insert a new menu item
    > menu.Insert(-1, position, "Menu Name")
    >
    > does not work.
    >


    The following comments are the result of a brief search of the web, so
    I'm not sure whether they're correct, but they might be worth a try.

    > full code:
    > def addnew(self, position, path):
    > path = path.strip()
    > try:
    > self.filelist.index(path) # index() throws ValueError
    > if path does not exist.
    > except:
    > # ValueError, path is not in list, so add it:
    > if position >= len(self.filelist):
    > self.filelist.append(path)
    > item = self.menu.Append(-1, path) #this does return a
    > menu item
    > self.window.Bind(wx.EVT_MENU, self.event, item)
    > else:
    > self.filelist.insert(position, path)
    > item = self.menu.InsertItem(-1, position, path) # does
    > Not return a menu item. throws some exception.


    I think it should be:

    item = self.menu.InsertItem(position, -1, path)

    > print item
    > #self.window.Bind(wx.EVT_MENU, self.event, item)
    >
    >
    > How do I remove a menu item from a menu? The RecentUsedList is eg: 4
    > element long. When opening a new file, this should be added at the
    > top, and the 4th element must be dropped.


    I think you need the 'Remove' method.
    MRAB, Aug 26, 2009
    #2
    1. Advertising

  3. BlueFlash

    John Ladasky Guest

    You might want to direct your wxPython questions to the dedicated
    wxPython newsgroup. It's Google-only, and thus not part of the Usenet
    hierarchy. But it's the most on-topic newsgroup you will find.

    http://groups.google.com/group/wxpython-users

    I attempted to crosspost this article to wx-python users, but that
    doesn't work for non-Usenet groups... Good luck!
    John Ladasky, Aug 26, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gino
    Replies:
    12
    Views:
    1,049
    Keith Wansbrough
    Sep 1, 2004
  2. nrm
    Replies:
    3
    Views:
    528
  3. Replies:
    0
    Views:
    1,432
  4. Ann
    Replies:
    0
    Views:
    151
  5. Replies:
    0
    Views:
    185
Loading...

Share This Page