/Demo/tix/samples/DirList.py

http://unladen-swallow.googlecode.com/ · Python · 131 lines · 64 code · 25 blank · 42 comment · 4 complexity · 6cdb8edda6390c706cac2e6a710797d0 MD5 · raw file

  1. # -*-mode: python; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
  2. #
  3. # $Id: DirList.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 tixDirList widget -- you can
  13. # use it for the user to select a directory. For example, an installation
  14. # program can use the tixDirList 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. dirlist = DemoDirList(root)
  22. dirlist.mainloop()
  23. dirlist.destroy()
  24. class DemoDirList:
  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 tixDirList 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 DirList widget. By default it will show the current
  36. # directory
  37. #
  38. #
  39. top.dir = Tix.DirList(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 DirList 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. font = self.root.tk.eval('tix option get fixed_font')
  56. # font = self.root.master.tix_option_get('fixed_font')
  57. top.ent.entry['font'] = font
  58. self.dlist_dir = copy.copy(os.curdir)
  59. # This should work setting the entry's textvariable
  60. top.ent.entry['textvariable'] = self.dlist_dir
  61. top.btn['command'] = lambda dir=top.dir, ent=top.ent, self=self: \
  62. self.copy_name(dir,ent)
  63. # top.ent.entry.insert(0,'tix'+repr(self))
  64. top.ent.entry.bind('<Return>', lambda self=self: self.okcmd () )
  65. top.pack( expand='yes', fill='both', side=TOP)
  66. top.dir.pack( expand=1, fill=BOTH, padx=4, pady=4, side=LEFT)
  67. top.btn.pack( anchor='s', padx=4, pady=4, side=LEFT)
  68. top.ent.pack( expand=1, fill=X, anchor='s', padx=4, pady=4, side=LEFT)
  69. # Use a ButtonBox to hold the buttons.
  70. #
  71. box = Tix.ButtonBox (w, orientation='horizontal')
  72. box.add ('ok', text='Ok', underline=0, width=6,
  73. command = lambda self=self: self.okcmd () )
  74. box.add ('cancel', text='Cancel', underline=0, width=6,
  75. command = lambda self=self: self.quitcmd () )
  76. box.pack( anchor='s', fill='x', side=BOTTOM)
  77. def copy_name (self, dir, ent):
  78. # This should work as it is the entry's textvariable
  79. self.dlist_dir = dir.cget('value')
  80. # but it isn't so I'll do it manually
  81. ent.entry.delete(0,'end')
  82. ent.entry.insert(0, self.dlist_dir)
  83. def okcmd (self):
  84. # tixDemo:Status "You have selected the directory" + self.dlist_dir
  85. self.quitcmd()
  86. def quitcmd (self):
  87. self.exit = 0
  88. def mainloop(self):
  89. while self.exit < 0:
  90. self.root.tk.dooneevent(TCL_ALL_EVENTS)
  91. def destroy (self):
  92. self.root.destroy()
  93. # This "if" statement makes it possible to run this script file inside or
  94. # outside of the main demo program "tixwidgets.py".
  95. #
  96. if __name__== '__main__' :
  97. import tkMessageBox, traceback
  98. try:
  99. root=Tix.Tk()
  100. RunSample(root)
  101. except:
  102. t, v, tb = sys.exc_info()
  103. text = "Error running the demo script:\n"
  104. for line in traceback.format_exception(t,v,tb):
  105. text = text + line + '\n'
  106. d = tkMessageBox.showerror ( 'Tix Demo Error', text)