/Pycon2009/internet/examples/ch19/listdir.py

https://github.com/toastdriven/pydanny-event-notes
Python | 115 lines | 97 code | 17 blank | 1 comment | 10 complexity | 7b99f9f5e0b96fc5944f54257009749e MD5 | raw file
  1. #!/usr/bin/env python
  2. import os
  3. from time import sleep
  4. from Tkinter import *
  5. class DirList:
  6. def __init__(self, initdir=None):
  7. self.top = Tk()
  8. self.label = Label(self.top, \
  9. text='Directory Lister' + ' v1.1')
  10. self.label.pack()
  11. self.cwd=StringVar(self.top)
  12. self.dirl = Label(self.top, fg='blue',
  13. font=('Helvetica', 12, 'bold'))
  14. self.dirl.pack()
  15. self.dirfm = Frame(self.top)
  16. self.dirsb = Scrollbar(self.dirfm)
  17. self.dirsb.pack(side=RIGHT, fill=Y)
  18. self.dirs = Listbox(self.dirfm, height=15, \
  19. width=50, yscrollcommand=self.dirsb.set)
  20. self.dirs.bind('<Double-1>', self.setdirandgo)
  21. self.dirsb.config(command=self.dirs.yview)
  22. self.dirs.pack(side=LEFT, fill=BOTH)
  23. self.dirfm.pack()
  24. self.dirn = Entry(self.top, width=50, \
  25. textvariable=self.cwd)
  26. self.dirn.bind('<Return>', self.dols)
  27. self.dirn.pack()
  28. self.bfm = Frame(self.top)
  29. self.clr = Button(self.bfm, text='Clear', \
  30. command=self.clrdir, \
  31. activeforeground='white', \
  32. activebackground='blue')
  33. self.ls = Button(self.bfm, \
  34. text='List Directory', \
  35. command=self.dols, \
  36. activeforeground='white', \
  37. activebackground='green')
  38. self.quit = Button(self.bfm, text='Quit', \
  39. command=self.top.quit, \
  40. activeforeground='white', \
  41. activebackground='red')
  42. self.clr.pack(side=LEFT)
  43. self.ls.pack(side=LEFT)
  44. self.quit.pack(side=LEFT)
  45. self.bfm.pack()
  46. if initdir:
  47. self.cwd.set(os.curdir)
  48. self.dols()
  49. def clrdir(self, ev=None):
  50. self.cwd.set('')
  51. def setdirandgo(self, ev=None):
  52. self.last = self.cwd.get()
  53. self.dirs.config(selectbackground='red')
  54. check = self.dirs.get(self.dirs.curselection())
  55. if not check:
  56. check = os.curdir
  57. self.cwd.set(check)
  58. self.dols()
  59. def dols(self, ev=None):
  60. error = ''
  61. tdir = self.cwd.get()
  62. if not tdir: tdir = os.curdir
  63. if not os.path.exists(tdir):
  64. error = tdir + ': no such file'
  65. elif not os.path.isdir(tdir):
  66. error = tdir + ': not a directory'
  67. if error:
  68. self.cwd.set(error)
  69. self.top.update()
  70. sleep(2)
  71. if not (hasattr(self, 'last') \
  72. and self.last):
  73. self.last = os.curdir
  74. self.cwd.set(self.last)
  75. self.dirs.config( \
  76. selectbackground='LightSkyBlue')
  77. self.top.update()
  78. return
  79. self.cwd.set( \
  80. 'FETCHING DIRECTORY CONTENTS...')
  81. self.top.update()
  82. dirlist = os.listdir(tdir)
  83. dirlist.sort()
  84. os.chdir(tdir)
  85. self.dirl.config(text=os.getcwd())
  86. self.dirs.delete(0, END)
  87. self.dirs.insert(END, os.curdir)
  88. self.dirs.insert(END, os.pardir)
  89. for eachFile in dirlist:
  90. self.dirs.insert(END, eachFile)
  91. self.cwd.set(os.curdir)
  92. self.dirs.config( \
  93. selectbackground='LightSkyBlue')
  94. def main():
  95. d = DirList(os.curdir)
  96. mainloop()
  97. if __name__ == '__main__':
  98. main()