I’m trying to implement a recent file list in my application, but I’m having some trouble. I’m not sure how recent file lists are conventionally implemented, so my method may be different.
First of all, I have a class RecentFileManager which takes care of manipulating the recent files file.
class RecentFileManager:
def init(self, filename):
self.file = file(filename, “r”)
def writeFile(self, filepath):
""" Writes a filepath to the recent file list.
The filepath should include the filename. """
self.file.write(filepath)
def getRecentFiles(self):
return self.file.readlines()
My first question is not wxPython-related, but I’m unable to find the answer anywhere. What file descriptor am I supposed to use for read-write access? I know it’s not “r”.
Anyway, using that RecentFileManager I dynamically generate a menu.
filerecentmenu = wx.Menu()
filemenu.AppendMenu(109, "&Recent Files", filerecentmenu)
# Recent file loop
for item in self.recent.getRecentFiles():
recentitem = filerecentmenu.Append(wx.NewId(), item.rstrip(), "")
self.Bind(wx.EVT_MENU, self.OnOpenRecent, recentitem)
The menu looks like it is generated properly, but there are a few problems. First, it binds every item in the Recent Files menu to a different action that’s completely unrelated to opening files (e.g. exiting, saving, etc.). Is this because I’m using wx.NewId(), and the application is binding to the wrong methods? I want it to bind to OnOpenRecent.
Second, I want each item in the Recent Files menu, when clicked, to open that file. I have everything set up, but I can’t figure out how to make OnOpenRecent recognize the menu item that is sending the event, and open the correct file in that manner. If I have three items in the Recent Files menu, I have no way of clicking on one of them and having the application recognize which one I clicked.
To recap:
- What’s the correct file descriptor for read-write access?
- How can I bind the dynamically generated menu items to a single method?
- How can I make this method recognize which menu item is sending the event?
If there’s an easier way to go about implementing recent file lists, I would like to hear about it.
Thank you for the assistance.
-Saketh