/Demo/tix/samples/Tree.py

http://unladen-swallow.googlecode.com/ · Python · 80 lines · 40 code · 8 blank · 32 comment · 10 complexity · 33b35ea1af91441dde260e8d66862455 MD5 · raw file

  1. # -*-mode: python; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
  2. #
  3. # $Id: Tree.py 36560 2004-07-18 06:16:08Z tim_one $
  4. #
  5. # Tix Demostration Program
  6. #
  7. # This sample program is structured in such a way so that it can be
  8. # executed from the Tix demo program "tixwidgets.py": it must have a
  9. # procedure called "RunSample". It should also have the "if" statment
  10. # at the end of this file so that it can be run as a standalone
  11. # program.
  12. # This file demonstrates how to use the TixTree widget to display
  13. # dynamic hierachical data (the files in the Unix file system)
  14. #
  15. import Tix, os
  16. def RunSample(w):
  17. top = Tix.Frame(w, relief=Tix.RAISED, bd=1)
  18. tree = Tix.Tree(top, options='separator "/"')
  19. tree.pack(expand=1, fill=Tix.BOTH, padx=10, pady=10, side=Tix.LEFT)
  20. tree['opencmd'] = lambda dir=None, w=tree: opendir(w, dir)
  21. # The / directory is added in the "open" mode. The user can open it
  22. # and then browse its subdirectories ...
  23. adddir(tree, "/")
  24. box = Tix.ButtonBox(w, orientation=Tix.HORIZONTAL)
  25. box.add('ok', text='Ok', underline=0, command=w.destroy, width=6)
  26. box.add('cancel', text='Cancel', underline=0, command=w.destroy, width=6)
  27. box.pack(side=Tix.BOTTOM, fill=Tix.X)
  28. top.pack(side=Tix.TOP, fill=Tix.BOTH, expand=1)
  29. def adddir(tree, dir):
  30. if dir == '/':
  31. text = '/'
  32. else:
  33. text = os.path.basename(dir)
  34. tree.hlist.add(dir, itemtype=Tix.IMAGETEXT, text=text,
  35. image=tree.tk.call('tix', 'getimage', 'folder'))
  36. try:
  37. os.listdir(dir)
  38. tree.setmode(dir, 'open')
  39. except os.error:
  40. # No read permission ?
  41. pass
  42. # This function is called whenever the user presses the (+) indicator or
  43. # double clicks on a directory whose mode is "open". It loads the files
  44. # inside that directory into the Tree widget.
  45. #
  46. # Note we didn't specify the closecmd option for the Tree widget, so it
  47. # performs the default action when the user presses the (-) indicator or
  48. # double clicks on a directory whose mode is "close": hide all of its child
  49. # entries
  50. def opendir(tree, dir):
  51. entries = tree.hlist.info_children(dir)
  52. if entries:
  53. # We have already loaded this directory. Let's just
  54. # show all the child entries
  55. #
  56. # Note: since we load the directory only once, it will not be
  57. # refreshed if the you add or remove files from this
  58. # directory.
  59. #
  60. for entry in entries:
  61. tree.hlist.show_entry(entry)
  62. files = os.listdir(dir)
  63. for file in files:
  64. if os.path.isdir(dir + '/' + file):
  65. adddir(tree, dir + '/' + file)
  66. else:
  67. tree.hlist.add(dir + '/' + file, itemtype=Tix.IMAGETEXT, text=file,
  68. image=tree.tk.call('tix', 'getimage', 'file'))
  69. if __name__ == '__main__':
  70. root = Tix.Tk()
  71. RunSample(root)
  72. root.mainloop()