/Demo/tix/samples/DirTree.py

http://unladen-swallow.googlecode.com/ · Python · 117 lines · 54 code · 23 blank · 40 comment · 2 complexity · 28dc238b9882d96795e6210c47285629 MD5 · raw file

  1. # -*-mode: python; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
  2. #
  3. # $Id: DirTree.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 using tixwish.
  12. # This file demonstrates the use of the tixDirTree widget -- you can
  13. # use it for the user to select a directory. For example, an installation
  14. # program can use the tixDirTree widget to ask the user to select the
  15. # installation directory for an application.
  16. #
  17. import Tix, os, copy
  18. from Tkconstants import *
  19. TCL_ALL_EVENTS = 0
  20. def RunSample (root):
  21. dirtree = DemoDirTree(root)
  22. dirtree.mainloop()
  23. dirtree.destroy()
  24. class DemoDirTree:
  25. def __init__(self, w):
  26. self.root = w
  27. self.exit = -1
  28. z = w.winfo_toplevel()
  29. z.wm_protocol("WM_DELETE_WINDOW", lambda self=self: self.quitcmd())
  30. # Create the tixDirTree and the tixLabelEntry widgets on the on the top
  31. # of the dialog box
  32. # bg = root.tk.eval('tix option get bg')
  33. # adding bg=bg crashes Windows pythonw tk8.3.3 Python 2.1.0
  34. top = Tix.Frame( w, relief=RAISED, bd=1)
  35. # Create the DirTree widget. By default it will show the current
  36. # directory
  37. #
  38. #
  39. top.dir = Tix.DirTree(top)
  40. top.dir.hlist['width'] = 40
  41. # When the user presses the ".." button, the selected directory
  42. # is "transferred" into the entry widget
  43. #
  44. top.btn = Tix.Button(top, text = " >> ", pady = 0)
  45. # We use a LabelEntry to hold the installation directory. The user
  46. # can choose from the DirTree widget, or he can type in the directory
  47. # manually
  48. #
  49. top.ent = Tix.LabelEntry(top, label="Installation Directory:",
  50. labelside = 'top',
  51. options = '''
  52. entry.width 40
  53. label.anchor w
  54. ''')
  55. self.dlist_dir = copy.copy(os.curdir)
  56. top.ent.entry['textvariable'] = self.dlist_dir
  57. top.btn['command'] = lambda dir=top.dir, ent=top.ent, self=self: \
  58. self.copy_name(dir,ent)
  59. top.ent.entry.bind('<Return>', lambda self=self: self.okcmd () )
  60. top.pack( expand='yes', fill='both', side=TOP)
  61. top.dir.pack( expand=1, fill=BOTH, padx=4, pady=4, side=LEFT)
  62. top.btn.pack( anchor='s', padx=4, pady=4, side=LEFT)
  63. top.ent.pack( expand=1, fill=X, anchor='s', padx=4, pady=4, side=LEFT)
  64. # Use a ButtonBox to hold the buttons.
  65. #
  66. box = Tix.ButtonBox (w, orientation='horizontal')
  67. box.add ('ok', text='Ok', underline=0, width=6,
  68. command = lambda self=self: self.okcmd () )
  69. box.add ('cancel', text='Cancel', underline=0, width=6,
  70. command = lambda self=self: self.quitcmd () )
  71. box.pack( anchor='s', fill='x', side=BOTTOM)
  72. def copy_name (self, dir, ent):
  73. # This should work as it is the entry's textvariable
  74. self.dlist_dir = dir.cget('value')
  75. # but it isn't so I'll do it manually
  76. ent.entry.delete(0,'end')
  77. ent.entry.insert(0, self.dlist_dir)
  78. def okcmd (self):
  79. # tixDemo:Status "You have selected the directory" + self.dlist_dir
  80. self.quitcmd()
  81. def quitcmd (self):
  82. # tixDemo:Status "You have selected the directory" + self.dlist_dir
  83. self.exit = 0
  84. def mainloop(self):
  85. while self.exit < 0:
  86. self.root.tk.dooneevent(TCL_ALL_EVENTS)
  87. def destroy (self):
  88. self.root.destroy()
  89. # This "if" statement makes it possible to run this script file inside or
  90. # outside of the main demo program "tixwidgets.py".
  91. #
  92. if __name__== '__main__' :
  93. root=Tix.Tk()
  94. RunSample(root)