[wxPython] wxFileHistory problem

Hi,

I've tryed to use file history in a MDI application
but when I open a second file an error appears:
   "wxMenu.SetLabel: no such item"
and the file menu doesn't show the entire list of
recent files for the last child frame

I use wxPython 2.3.3.1 for Python 2.2 on win98

Best regards,
  Lucian.

Here is a sample:

from wxPython.wx import *
FileHistoryManager = None
  
def InitializeFileMenu (window, OnOpenFile):
  global FileHistoryManager
  fileMenu = wxMenu ()
  openId = wxNewId ()
  fileMenu.Append (openId, "Open file")
  EVT_MENU (window, openId, OnOpenFile)
  
  if not FileHistoryManager:
    FileHistoryManager = wxFileHistory ()
  return fileMenu

def RemoveFromFileHistory (menu):
  FileHistoryManager.RemoveMenu (menu)
    
def OpenFile (mainFrame):
  dlg = wxFileDialog (mainFrame, style=wxOPEN)
  rez = dlg.ShowModal ()
  if rez == wxID_OK:
    filepath = dlg.GetPath ()
  else:
    return
  frame = DataFrame (filepath, mainFrame)
  AddANewHistoryMenu (frame.GetMenuBar().GetMenu (0),
filepath)
  
def AddANewHistoryMenu (menu, filepath=None):
  FileHistoryManager.UseMenu (menu)
  if filepath:
    FileHistoryManager.AddFileToHistory (filepath)
  
class DataFrame(wxMDIChildFrame):
  def __init__(self, filepath, parent):
    wxMDIChildFrame.__init__(self, parent, -1,
'CHILD_FRAME', style=wxDEFAULT_FRAME_STYLE)
    
    self.SetTitle(filepath)
    self.parent = parent
          
    #initialize MenuBar
    self.SetMenuBar (self.InitializeMenuBar ())

    EVT_CLOSE (self, self.OnCloseWindow)
    
  def InitializeMenuBar (self):
    menuBar = wxMenuBar ()
    fileMenu = InitializeFileMenu (self,
self.OnOpenFile)
    menuBar.Append (fileMenu, "&File")
  
    return menuBar

  def OnOpenFile (self, evt):
    OpenFile (self.parent)
    
  def OnCloseWindow (self, evt):
    RemoveFromFileHistory (self.GetMenuBar ().GetMenu
(0))
    self.Destroy ()

class MainFrame(wxMDIParentFrame):
  def __init__(self, parent=None):
    wxMDIParentFrame.__init__(self, parent, -1,
'MAIN_FRAME', style=wxDEFAULT_FRAME_STYLE)
            
    #initialize MenuBar
    self.SetMenuBar (self.InitializeMenuBar ())
    AddANewHistoryMenu (self.GetMenuBar ().GetMenu (0))
     
    EVT_CLOSE (self, self.OnCloseWindow)
    self.Maximize ()

  def InitializeMenuBar (self):
    menuBar = wxMenuBar ()
    fileMenu = InitializeFileMenu (self,
self.OnOpenFile)
    menuBar.Append (fileMenu, "&File")
    return menuBar

  def OnOpenFile (self, evt):
    OpenFile (self)

  def OnCloseWindow (self, evt):
    self.Destroy ()

class App(wxApp):
  def OnInit(self):
    frame = MainFrame ()
    self.SetTopWindow(frame)
    frame.Show (true)
    return true

app = App(0)
app.MainLoop()

···

__________________________________________________
Do you Yahoo!?
Faith Hill - Exclusive Performances, Videos & More
http://faith.yahoo.com

Lucian Marinescu wrote:

Hi,

I've tryed to use file history in a MDI application
but when I open a second file an error appears:
   "wxMenu.SetLabel: no such item"
and the file menu doesn't show the entire list of
recent files for the last child frame

Since you are creating a new menu for each child window, you need to add the files already in the history to each one. This change will do it for you:

def AddANewHistoryMenu (menu, filepath=None):
  FileHistoryManager.UseMenu (menu)
         FileHistoryManager.AddFilesToThisMenu(menu)
  if filepath:
    FileHistoryManager.AddFileToHistory (filepath)

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!