dirWidget2.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/python
  2. ''' a widget that shows only a dir listing
  3. '''
  4. import sys,os,argparse
  5. from Tkinter import *
  6. class DirWidget(Listbox):
  7. ''' a simple widget that shows a list of directories, staring
  8. at the directory passed into the constructor
  9. a mouse click or 'enter' key will send a 'selected' signal to
  10. anyone who connects to this.
  11. the .. parent directory is shown for all dirs except /
  12. the most interesting parts of the interface are:
  13. constructor - send in the directory to view
  14. method - currentPath() returns text of current path
  15. new event <<DirChanged>>
  16. '''
  17. def __init__(self, parent, directory='.'):
  18. Listbox.__init__(self,parent)
  19. self.directory = directory
  20. self.refill()
  21. self.bind("<Key>",self.quickFind)
  22. self.bind("<Double-Button-1>",self.hit)
  23. self.bind("<Return>",self.hit)
  24. def hit(self,event):
  25. if self.size()==0:
  26. return
  27. self.directory = os.path.abspath(self.directory + '/' + self.get(ACTIVE))
  28. self.refill()
  29. self.event_generate("<<DirChanged>>")
  30. def refill(self):
  31. current,dirs,files = os.walk(self.directory,followlinks=True).next()
  32. dirs = sorted(dirs, key=lambda s: s.lower())
  33. if '/' not in dirs:
  34. dirs = ['..'] + dirs
  35. self.delete(0,END)
  36. for d in dirs:
  37. self.insert(END,d)
  38. #self.selection_set(0)
  39. def currentPath(self):
  40. return self.directory
  41. def quickFind(self,event):
  42. if event.char==None or event.char=='' or event.char==chr(13):
  43. return
  44. event.char = event.char.lower()
  45. if event.char not in 'abcdefghijklmnopqrstuvwxyz0123456789_-.%':
  46. return
  47. for i in range(self.size()):
  48. if self.get(i)[0].lower()==event.char:
  49. self.activate(i)
  50. self.see(i)
  51. return
  52. if __name__=="__main__":
  53. def alert(some_event):
  54. print("dirWidget hit dir: " + some_event.widget.currentPath())
  55. os.system('notify-send -t 1500 "file: ' +
  56. some_event.widget.currentPath() + '"')
  57. t = Tk()
  58. dw = DirWidget(t)
  59. dw.pack()
  60. t.bind("<<DirChanged>>",alert)
  61. Button(t,text="quit",command=t.destroy).pack()
  62. mainloop()
  63. '''
  64. notes on Listbox
  65. properties: can use []
  66. archivestyle, bg, bd, cursor, disabledforeground, exportselection, font,
  67. fg, height/width, highlightbacground, listvariable, relief, selectbackground,
  68. selectforground, selectmode, state, takefocus, x(y)scrollcommand
  69. methods:
  70. activate(i),bbox(i),curselection(),delete(i,j),get(i,j),index(i),
  71. insert(i,data), itemcget(i), itemconfig(i,option),nearest(y),scan_dragto(x,y),
  72. scan_mark(x,y),see(i),selection_anchor(i),selection_clear(i,j),
  73. selection_includes(i,j),size(),x/yview(),x/yview_moveto(),x/yview_scroll(),
  74. inherit-methods:
  75. after(t,cb),after_cancel(id),after_idle(cb),bell(),bind(seq,cb),
  76. bind_all(seq,cb),bind_class(cn,seq,cb),bindtags(),cget(opt),clipboard_append(t),
  77. clipboard_clear(),column_configure(),config[ure](opt),destroy(),
  78. event_add(v,s),event_delete(v,s),event_generate(s,kw),event_info(),
  79. focus_displayof(),_force(),_get(),_lastfor(),_set(),
  80. grab_current(),_release(),_set(),_set_global(),_status(),
  81. grid_forget(),_propagate(),_remove(),
  82. image_names(),keys(),lift(),lower(),mainloop(),nametowidget(n),
  83. option_add(),_clear(),_get(),_readfile(),
  84. register(f),quit(),rowconfigure(),selection_clear(),_get(),_own(),_own_get(),
  85. tk_focusFollowsMouse(),_focusNext(),_focusPrev(),
  86. unbind(),unbind_all(),unbind_class(),update,update_idletasks(),
  87. wait_variable(v),_visibility(w),_window(w),
  88. winfo_children(),_class(),_containing(),_depth(),_fpixels(n),_geometry(),
  89. _height(),_id(),_ismapped(),_manager(),_name(),_parent(),_pathname(),_pixels(),
  90. _pointerx/y(),_reqheight/width(),_rgb(color),_rootx/y(),_screenheight/width(),
  91. _screenmmheight/width(),_screenvisual(),_toplevel(),_viewable(),x/y()
  92. '''