#!/usr/bin/python ''' a widget that shows only a dir listing ''' import sys,os,argparse from Tkinter import * class DirWidget(Listbox): ''' a simple widget that shows a list of directories, staring at the directory passed into the constructor a mouse click or 'enter' key will send a 'selected' signal to anyone who connects to this. the .. parent directory is shown for all dirs except / the most interesting parts of the interface are: constructor - send in the directory to view method - currentPath() returns text of current path new event <> ''' def __init__(self, parent, directory='.'): Listbox.__init__(self,parent) self.directory = directory self.refill() self.bind("",self.quickFind) self.bind("",self.hit) self.bind("",self.hit) def hit(self,event): if self.size()==0: return self.directory = os.path.abspath(self.directory + '/' + self.get(ACTIVE)) self.refill() self.event_generate("<>") def refill(self): current,dirs,files = os.walk(self.directory,followlinks=True).next() dirs = sorted(dirs, key=lambda s: s.lower()) if '/' not in dirs: dirs = ['..'] + dirs self.delete(0,END) for d in dirs: self.insert(END,d) #self.selection_set(0) def currentPath(self): return self.directory def quickFind(self,event): if event.char==None or event.char=='' or event.char==chr(13): return event.char = event.char.lower() if event.char not in 'abcdefghijklmnopqrstuvwxyz0123456789_-.%': return for i in range(self.size()): if self.get(i)[0].lower()==event.char: self.activate(i) self.see(i) return if __name__=="__main__": def alert(some_event): print("dirWidget hit dir: " + some_event.widget.currentPath()) os.system('notify-send -t 1500 "file: ' + some_event.widget.currentPath() + '"') t = Tk() dw = DirWidget(t) dw.pack() t.bind("<>",alert) Button(t,text="quit",command=t.destroy).pack() mainloop() ''' notes on Listbox properties: can use [] archivestyle, bg, bd, cursor, disabledforeground, exportselection, font, fg, height/width, highlightbacground, listvariable, relief, selectbackground, selectforground, selectmode, state, takefocus, x(y)scrollcommand methods: activate(i),bbox(i),curselection(),delete(i,j),get(i,j),index(i), insert(i,data), itemcget(i), itemconfig(i,option),nearest(y),scan_dragto(x,y), scan_mark(x,y),see(i),selection_anchor(i),selection_clear(i,j), selection_includes(i,j),size(),x/yview(),x/yview_moveto(),x/yview_scroll(), inherit-methods: after(t,cb),after_cancel(id),after_idle(cb),bell(),bind(seq,cb), bind_all(seq,cb),bind_class(cn,seq,cb),bindtags(),cget(opt),clipboard_append(t), clipboard_clear(),column_configure(),config[ure](opt),destroy(), event_add(v,s),event_delete(v,s),event_generate(s,kw),event_info(), focus_displayof(),_force(),_get(),_lastfor(),_set(), grab_current(),_release(),_set(),_set_global(),_status(), grid_forget(),_propagate(),_remove(), image_names(),keys(),lift(),lower(),mainloop(),nametowidget(n), option_add(),_clear(),_get(),_readfile(), register(f),quit(),rowconfigure(),selection_clear(),_get(),_own(),_own_get(), tk_focusFollowsMouse(),_focusNext(),_focusPrev(), unbind(),unbind_all(),unbind_class(),update,update_idletasks(), wait_variable(v),_visibility(w),_window(w), winfo_children(),_class(),_containing(),_depth(),_fpixels(n),_geometry(), _height(),_id(),_ismapped(),_manager(),_name(),_parent(),_pathname(),_pixels(), _pointerx/y(),_reqheight/width(),_rgb(color),_rootx/y(),_screenheight/width(), _screenmmheight/width(),_screenvisual(),_toplevel(),_viewable(),x/y() '''