I have started working on a new opensource project, using pygtk and Python (details on the project at a later stage). I wanted to add the ability to select recent files from the main menu. While pygtk provides classes to enable this (RecentManager, RecentChooserMenu), I couldn't find any code examples. So here is an example that adds a Recent Files submenu to a menu item.
The code assumes there is an existing menu item 'self.mnuRecent' that the recent files sub menu will be attached to.
#Add a recent projects menu item
manager = gtk.recent_manager_get_default()
#define a RecentChooserMenu object
recent_menu_chooser = gtk.RecentChooserMenu(manager)
#define a file filter, otherwise all file types will show up
filter = gtk.RecentFilter()
filter.add_pattern("*.avi") #set this to whatever file type you want
#add the filter to the RecentChooserMenu object
recent_menu_chooser.add_filter(filter)
#add a signal to open the selected file
recent_menu_chooser.connect('item-activated', self.recent_item_activated)
#Attach the RecentChooserMenu to the main menu item
mnurecent = self.mnuRecent
mnurecent.set_submenu(recent_menu_chooser)
##
def recent_item_activated(self, widget):
"""Activated when an item from the recent projects menu is clicked"""
uri = widget.get_current_item().get_uri()
# Strip 'file://' from the beginning of the uri
file_to_open = uri[7:]
#code here to open the selected file
## In your code where you open a file, you need to add the file to the recent items manager
manager = gtk.recent_manager_get_default()
manager.add_item('file://' + file_uri)
Hopefully someone will find this example useful.
Showing posts with label pygtk. Show all posts
Showing posts with label pygtk. Show all posts
Friday, November 21, 2008
pygtk Recent File Chooser
Posted by
Andy
at
9:24 AM
2
comments
DiggIt!
Del.icio.us
Subscribe to:
Posts (Atom)