home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 14 / hacker14.iso / programacao / pythonwin / python.exe / TKFILEDIALOG.PY < prev    next >
Encoding:
Python Source  |  2003-06-15  |  5.6 KB  |  211 lines

  1. #
  2. # Instant Python
  3. # $Id: tkFileDialog.py,v 1.11 2003/06/15 19:08:57 gvanrossum 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. # - multiple: if true user may select more than one file
  33. #
  34. # options for the directory chooser:
  35. #
  36. # - initialdir, parent, title: see above
  37. #
  38. # - mustexist: if true, user must pick an existing directory
  39. #
  40. #
  41.  
  42.  
  43. from tkCommonDialog import Dialog
  44.  
  45. class _Dialog(Dialog):
  46.  
  47.     def _fixoptions(self):
  48.         try:
  49.             # make sure "filetypes" is a tuple
  50.             self.options["filetypes"] = tuple(self.options["filetypes"])
  51.         except KeyError:
  52.             pass
  53.  
  54.     def _fixresult(self, widget, result):
  55.         if result:
  56.             # keep directory and filename until next time
  57.             import os
  58.             # convert Tcl path objects to strings
  59.             try:
  60.                 result = result.string
  61.             except AttributeError:
  62.                 # it already is a string
  63.                 pass
  64.             path, file = os.path.split(result)
  65.             self.options["initialdir"] = path
  66.             self.options["initialfile"] = file
  67.         self.filename = result # compatibility
  68.         return result
  69.  
  70.  
  71. #
  72. # file dialogs
  73.  
  74. class Open(_Dialog):
  75.     "Ask for a filename to open"
  76.  
  77.     command = "tk_getOpenFile"
  78.  
  79.     def _fixresult(self, widget, result):
  80.         if isinstance(result, tuple):
  81.             # multiple results:
  82.             result = tuple([getattr(r, "string", r) for r in result])
  83.             if result:
  84.                 import os
  85.                 path, file = os.path.split(result[0])
  86.                 self.options["initialdir"] = path
  87.                 # don't set initialfile or filename, as we have multiple of these
  88.             return result
  89.         if not widget.tk.wantobjects() and "multiple" in self.options:
  90.             # Need to split result explicitly
  91.             return self._fixresult(widget, widget.tk.splitlist(result))
  92.         return _Dialog._fixresult(self, widget, result)
  93.  
  94. class SaveAs(_Dialog):
  95.     "Ask for a filename to save as"
  96.  
  97.     command = "tk_getSaveFile"
  98.  
  99.  
  100. # the directory dialog has its own _fix routines.
  101. class Directory(Dialog):
  102.     "Ask for a directory"
  103.  
  104.     command = "tk_chooseDirectory"
  105.  
  106.     def _fixresult(self, widget, result):
  107.         if result:
  108.             # keep directory until next time
  109.             self.options["initialdir"] = result
  110.         self.directory = result # compatibility
  111.         return result
  112.  
  113. #
  114. # convenience stuff
  115.  
  116. def askopenfilename(**options):
  117.     "Ask for a filename to open"
  118.  
  119.     return Open(**options).show()
  120.  
  121. def asksaveasfilename(**options):
  122.     "Ask for a filename to save as"
  123.  
  124.     return SaveAs(**options).show()
  125.  
  126. def askopenfilenames(**options):
  127.     """Ask for multiple filenames to open
  128.     
  129.     Returns a list of filenames or empty list if 
  130.     cancel button selected
  131.     """
  132.     options["multiple"]=1
  133.     return Open(**options).show()
  134.  
  135. # FIXME: are the following  perhaps a bit too convenient?
  136.  
  137. def askopenfile(mode = "r", **options):
  138.     "Ask for a filename to open, and returned the opened file"
  139.  
  140.     filename = Open(**options).show()
  141.     if filename:
  142.         return open(filename, mode)
  143.     return None
  144.  
  145. def askopenfiles(mode = "r", **options):
  146.     """Ask for multiple filenames and return the open file
  147.     objects
  148.     
  149.     returns a list of open file objects or an empty list if 
  150.     cancel selected
  151.     """
  152.  
  153.     files = askopenfilenames(**options)
  154.     if files:
  155.         ofiles=[]
  156.         for filename in files:
  157.             ofiles.append(open(filename, mode))
  158.         files=ofiles
  159.     return files
  160.  
  161.  
  162. def asksaveasfile(mode = "w", **options):
  163.     "Ask for a filename to save as, and returned the opened file"
  164.  
  165.     filename = SaveAs(**options).show()
  166.     if filename:
  167.         return open(filename, mode)
  168.     return None
  169.  
  170. def askdirectory (**options):
  171.     "Ask for a directory, and return the file name"
  172.     return Directory(**options).show()
  173.  
  174. # --------------------------------------------------------------------
  175. # test stuff
  176.  
  177. if __name__ == "__main__":
  178.     # Since the file name may contain non-ASCII characters, we need
  179.     # to find an encoding that likely supports the file name, and
  180.     # displays correctly on the terminal.
  181.  
  182.     # Start off with UTF-8
  183.     enc = "utf-8"
  184.     import sys
  185.  
  186.     # See whether CODESET is defined
  187.     try:
  188.         import locale
  189.         locale.setlocale(locale.LC_ALL,'')
  190.         enc = locale.nl_langinfo(locale.CODESET)
  191.     except (ImportError, AttributeError):
  192.         pass
  193.  
  194.     # dialog for openening files
  195.  
  196.     openfilename=askopenfilename(filetypes=[("all files", "*")])
  197.     try:
  198.         fp=open(openfilename,"r")
  199.         fp.close()
  200.     except:
  201.         print "Could not open File: " 
  202.         print sys.exc_info()[1]
  203.  
  204.     print "open", openfilename.encode(enc)
  205.  
  206.     # dialog for saving files
  207.  
  208.     saveasfilename=asksaveasfilename()
  209.     print "saveas", saveasfilename.encode(enc)
  210.  
  211.