dirWidget2.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/python3
  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. signal - selected calls a slot with a single arg: new path
  16. '''
  17. def __init__(self, parent, directory='.'):
  18. super(DirWidget,self).__init__(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. #li = QListWidgetItem(d,self)
  38. # replace this with an insert
  39. self.insert(END,d)
  40. self.selection_set(0)
  41. def currentPath(self):
  42. return self.directory
  43. def quickFind(self,event):
  44. if event.char==None or event.char=='' or event.char==chr(13):
  45. return
  46. event.char = event.char.lower()
  47. if event.char not in 'abcdefghijklmnopqrstuvwxyz0123456789_-.%':
  48. return
  49. print("quickFind: "+event.char+" ")
  50. for i in range(self.size()):
  51. print("qf get: " + self.get(i))
  52. if self.get(i)[0].lower()==event.char:
  53. self.activate(i)
  54. self.see(i)
  55. return
  56. if __name__=="__main__":
  57. def alert(some_event):
  58. print("dirWidget hit" + str(some_event))
  59. os.system('notify-send -t 1500 "file: ' +
  60. some_event.widget.currentPath() + '"')
  61. t = Tk()
  62. dw = DirWidget(t)
  63. dw.pack()
  64. t.bind("<<DirChanged>>",alert)
  65. Button(t,text="quit",command=t.destroy).pack()
  66. mainloop()
  67. '''
  68. notes on Listbox
  69. properties: can use []
  70. archivestyle, bg, bd, cursor, disabledforeground, exportselection, font,
  71. fg, height/width, highlightbacground, listvariable, relief, selectbackground,
  72. selectforground, selectmode, state, takefocus, x(y)scrollcommand
  73. methods:
  74. activate(i),bbox(i),curselection(),delete(i,j),get(i,j),index(i),
  75. insert(i,data), itemcget(i), itemconfig(i,option),nearest(y),scan_dragto(x,y),
  76. scan_mark(x,y),see(i),selection_anchor(i),selection_clear(i,j),
  77. selection_includes(i,j),size(),x/yview(),x/yview_moveto(),x/yview_scroll(),
  78. inherit-methods:
  79. after(t,cb),after_cancel(id),after_idle(cb),bell(),bind(seq,cb),
  80. bind_all(seq,cb),bind_class(cn,seq,cb),bindtags(),cget(opt),clipboard_append(t),
  81. clipboard_clear(),column_configure(),config[ure](opt),destroy(),
  82. event_add(v,s),event_delete(v,s),event_generate(s,kw),event_info(),
  83. focus_displayof(),_force(),_get(),_lastfor(),_set(),
  84. grab_current(),_release(),_set(),_set_global(),_status(),
  85. grid_forget(),_propagate(),_remove(),
  86. image_names(),keys(),lift(),lower(),mainloop(),nametowidget(n),
  87. option_add(),_clear(),_get(),_readfile(),
  88. register(f),quit(),rowconfigure(),selection_clear(),_get(),_own(),_own_get(),
  89. tk_focusFollowsMouse(),_focusNext(),_focusPrev(),
  90. unbind(),unbind_all(),unbind_class(),update,update_idletasks(),
  91. wait_variable(v),_visibility(w),_window(w),
  92. winfo_children(),_class(),_containing(),_depth(),_fpixels(n),_geometry(),
  93. _height(),_id(),_ismapped(),_manager(),_name(),_parent(),_pathname(),_pixels(),
  94. _pointerx/y(),_reqheight/width(),_rgb(color),_rootx/y(),_screenheight/width(),
  95. _screenmmheight/width(),_screenvisual(),_toplevel(),_viewable(),x/y()
  96. '''