home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 2004 #2 / K-CD-2-2004.ISO / OpenOffice Sv / f_0397 / python-core-2.2.2 / lib / lib-tk / tkFileDialog.py < prev    next >
Encoding:
Python Source  |  2003-07-18  |  3.1 KB  |  130 lines

  1. #
  2. # Instant Python
  3. # $Id: tkFileDialog.py,v 1.4 2001/11/08 17:51:33 loewis Exp $
  4. #
  5. # tk common file dialogues
  6. #
  7. # this module provides interfaces to the native file dialogues
  8. # available in Tk 4.2 and newer, and the directory dialogue available
  9. # in Tk 8.3 and newer.
  10. #
  11. # written by Fredrik Lundh, May 1997.
  12. #
  13.  
  14. #
  15. # options (all have default values):
  16. #
  17. # - defaultextension: added to filename if not explicitly given
  18. #
  19. # - filetypes: sequence of (label, pattern) tuples.  the same pattern
  20. #   may occur with several patterns.  use "*" as pattern to indicate
  21. #   all files.
  22. #
  23. # - initialdir: initial directory.  preserved by dialog instance.
  24. #
  25. # - initialfile: initial file (ignored by the open dialog).  preserved
  26. #   by dialog instance.
  27. #
  28. # - parent: which window to place the dialog on top of
  29. #
  30. # - title: dialog title
  31. #
  32. # options for the directory chooser:
  33. #
  34. # - initialdir, parent, title: see above
  35. #
  36. # - mustexist: if true, user must pick an existing directory
  37. #
  38.  
  39. from tkCommonDialog import Dialog
  40.  
  41. class _Dialog(Dialog):
  42.  
  43.     def _fixoptions(self):
  44.         try:
  45.             # make sure "filetypes" is a tuple
  46.             self.options["filetypes"] = tuple(self.options["filetypes"])
  47.         except KeyError:
  48.             pass
  49.  
  50.     def _fixresult(self, widget, result):
  51.         if result:
  52.             # keep directory and filename until next time
  53.             import os
  54.             path, file = os.path.split(result)
  55.             self.options["initialdir"] = path
  56.             self.options["initialfile"] = file
  57.         self.filename = result # compatibility
  58.         return result
  59.  
  60.  
  61. #
  62. # file dialogs
  63.  
  64. class Open(_Dialog):
  65.     "Ask for a filename to open"
  66.  
  67.     command = "tk_getOpenFile"
  68.  
  69. class SaveAs(_Dialog):
  70.     "Ask for a filename to save as"
  71.  
  72.     command = "tk_getSaveFile"
  73.  
  74.  
  75. # the directory dialog has its own _fix routines.
  76. class Directory(Dialog):
  77.     "Ask for a directory"
  78.  
  79.     command = "tk_chooseDirectory"
  80.  
  81.     def _fixresult(self, widget, result):
  82.         if result:
  83.             # keep directory until next time
  84.             self.options["initialdir"] = result
  85.         self.directory = result # compatibility
  86.         return result
  87.  
  88. #
  89. # convenience stuff
  90.  
  91. def askopenfilename(**options):
  92.     "Ask for a filename to open"
  93.  
  94.     return Open(**options).show()
  95.  
  96. def asksaveasfilename(**options):
  97.     "Ask for a filename to save as"
  98.  
  99.     return SaveAs(**options).show()
  100.  
  101. # FIXME: are the following two perhaps a bit too convenient?
  102.  
  103. def askopenfile(mode = "r", **options):
  104.     "Ask for a filename to open, and returned the opened file"
  105.  
  106.     filename = Open(**options).show()
  107.     if filename:
  108.         return open(filename, mode)
  109.     return None
  110.  
  111. def asksaveasfile(mode = "w", **options):
  112.     "Ask for a filename to save as, and returned the opened file"
  113.  
  114.     filename = SaveAs(**options).show()
  115.     if filename:
  116.         return open(filename, mode)
  117.     return None
  118.  
  119. def askdirectory (**options):
  120.     "Ask for a directory, and return the file name"
  121.     return Directory(**options).show()
  122.  
  123. # --------------------------------------------------------------------
  124. # test stuff
  125.  
  126. if __name__ == "__main__":
  127.  
  128.     print "open", askopenfilename(filetypes=[("all filez", "*")])
  129.     print "saveas", asksaveasfilename()
  130.