Skip to content

Instantly share code, notes, and snippets.

@koohz
Created December 28, 2013 12:35
Show Gist options
  • Save koohz/8159085 to your computer and use it in GitHub Desktop.
Save koohz/8159085 to your computer and use it in GitHub Desktop.
from gi.repository import Gtk, GObject
from gi.repository.GdkPixbuf import Pixbuf
icons = ["gtk-cut", "gtk-paste", "gtk-copy"]
class IconViewWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.set_default_size(200, 200)
liststore = Gtk.ListStore(Pixbuf, str)
iconview = Gtk.IconView.new()
iconview.set_model(liststore)
iconview.set_pixbuf_column(0)
iconview.set_text_column(1)
iconview.connect("key-press-event", self.on_button_press)
for icon in icons:
pixbuf = Gtk.IconTheme.get_default().load_icon(icon, 32, 0)
liststore.append([pixbuf, icon.replace("gtk-", "")])
self.connect("delete-event", Gtk.main_quit)
self.add(iconview)
self.show_all()
self._reset_timeout()
def on_button_press(self, widget, event):
if event.string.isalpha(): # You need some more validation on inputs
self._reset_timeout() # Reset time out preventing search_str to be resetted
self.search_str += event.string # Update search_str with the new char pressed
model = widget.get_model() # Retrieve liststore
for item in model: # Iterating Rows
label = list(item)[1] # getting label str
if label.startswith( self.search_str ): #check if label starts with search_str
widget.select_path(item.path) # let's iconview select
# some other stuff methods like iconview.scroll_to_path() etc...
def _check_timeout(self):
self.search_str = ''
return False # To kill GObject.add_timeout, callback must return false
search_str = ''
timer_id = False
def _reset_timeout(self):
if self.timer_id:
GObject.source_remove(self.timer_id)
self.timer_id = GObject.timeout_add(2000, self._check_timeout)
IconViewWindow()
Gtk.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment