0001 # 0002 # Instant Python 0003 # $Id: tkFileDialog.py,v 1.13 2004/07/18 06:14:44 tim_one Exp $ 0004 # 0005 # tk common file dialogues 0006 # 0007 # this module provides interfaces to the native file dialogues 0008 # available in Tk 4.2 and newer, and the directory dialogue available 0009 # in Tk 8.3 and newer. 0010 # 0011 # written by Fredrik Lundh, May 1997. 0012 # 0013 0014 # 0015 # options (all have default values): 0016 # 0017 # - defaultextension: added to filename if not explicitly given 0018 # 0019 # - filetypes: sequence of (label, pattern) tuples. the same pattern 0020 # may occur with several patterns. use "*" as pattern to indicate 0021 # all files. 0022 # 0023 # - initialdir: initial directory. preserved by dialog instance. 0024 # 0025 # - initialfile: initial file (ignored by the open dialog). preserved 0026 # by dialog instance. 0027 # 0028 # - parent: which window to place the dialog on top of 0029 # 0030 # - title: dialog title 0031 # 0032 # - multiple: if true user may select more than one file 0033 # 0034 # options for the directory chooser: 0035 # 0036 # - initialdir, parent, title: see above 0037 # 0038 # - mustexist: if true, user must pick an existing directory 0039 # 0040 # 0041 0042 0043 from tkCommonDialog import Dialog 0044 0045 class _Dialog(Dialog): 0046 0047 def _fixoptions(self): 0048 try: 0049 # make sure "filetypes" is a tuple 0050 self.options["filetypes"] = tuple(self.options["filetypes"]) 0051 except KeyError: 0052 pass 0053 0054 def _fixresult(self, widget, result): 0055 if result: 0056 # keep directory and filename until next time 0057 import os 0058 # convert Tcl path objects to strings 0059 try: 0060 result = result.string 0061 except AttributeError: 0062 # it already is a string 0063 pass 0064 path, file = os.path.split(result) 0065 self.options["initialdir"] = path 0066 self.options["initialfile"] = file 0067 self.filename = result # compatibility 0068 return result 0069 0070 0071 # 0072 # file dialogs 0073 0074 class Open(_Dialog): 0075 "Ask for a filename to open" 0076 0077 command = "tk_getOpenFile" 0078 0079 def _fixresult(self, widget, result): 0080 if isinstance(result, tuple): 0081 # multiple results: 0082 result = tuple([getattr(r, "string", r) for r in result]) 0083 if result: 0084 import os 0085 path, file = os.path.split(result[0]) 0086 self.options["initialdir"] = path 0087 # don't set initialfile or filename, as we have multiple of these 0088 return result 0089 if not widget.tk.wantobjects() and "multiple" in self.options: 0090 # Need to split result explicitly 0091 return self._fixresult(widget, widget.tk.splitlist(result)) 0092 return _Dialog._fixresult(self, widget, result) 0093 0094 class SaveAs(_Dialog): 0095 "Ask for a filename to save as" 0096 0097 command = "tk_getSaveFile" 0098 0099 0100 # the directory dialog has its own _fix routines. 0101 class Directory(Dialog): 0102 "Ask for a directory" 0103 0104 command = "tk_chooseDirectory" 0105 0106 def _fixresult(self, widget, result): 0107 if result: 0108 # convert Tcl path objects to strings 0109 try: 0110 result = result.string 0111 except AttributeError: 0112 # it already is a string 0113 pass 0114 # keep directory until next time 0115 self.options["initialdir"] = result 0116 self.directory = result # compatibility 0117 return result 0118 0119 # 0120 # convenience stuff 0121 0122 def askopenfilename(**options): 0123 "Ask for a filename to open" 0124 0125 return Open(**options).show() 0126 0127 def asksaveasfilename(**options): 0128 "Ask for a filename to save as" 0129 0130 return SaveAs(**options).show() 0131 0132 def askopenfilenames(**options): 0133 """Ask for multiple filenames to open 0134 0135 Returns a list of filenames or empty list if 0136 cancel button selected 0137 """ 0138 options["multiple"]=1 0139 return Open(**options).show() 0140 0141 # FIXME: are the following perhaps a bit too convenient? 0142 0143 def askopenfile(mode = "r", **options): 0144 "Ask for a filename to open, and returned the opened file" 0145 0146 filename = Open(**options).show() 0147 if filename: 0148 return open(filename, mode) 0149 return None 0150 0151 def askopenfiles(mode = "r", **options): 0152 """Ask for multiple filenames and return the open file 0153 objects 0154 0155 returns a list of open file objects or an empty list if 0156 cancel selected 0157 """ 0158 0159 files = askopenfilenames(**options) 0160 if files: 0161 ofiles=[] 0162 for filename in files: 0163 ofiles.append(open(filename, mode)) 0164 files=ofiles 0165 return files 0166 0167 0168 def asksaveasfile(mode = "w", **options): 0169 "Ask for a filename to save as, and returned the opened file" 0170 0171 filename = SaveAs(**options).show() 0172 if filename: 0173 return open(filename, mode) 0174 return None 0175 0176 def askdirectory (**options): 0177 "Ask for a directory, and return the file name" 0178 return Directory(**options).show() 0179 0180 # -------------------------------------------------------------------- 0181 # test stuff 0182 0183 if __name__ == "__main__": 0184 # Since the file name may contain non-ASCII characters, we need 0185 # to find an encoding that likely supports the file name, and 0186 # displays correctly on the terminal. 0187 0188 # Start off with UTF-8 0189 enc = "utf-8" 0190 import sys 0191 0192 # See whether CODESET is defined 0193 try: 0194 import locale 0195 locale.setlocale(locale.LC_ALL,'') 0196 enc = locale.nl_langinfo(locale.CODESET) 0197 except (ImportError, AttributeError): 0198 pass 0199 0200 # dialog for openening files 0201 0202 openfilename=askopenfilename(filetypes=[("all files", "*")]) 0203 try: 0204 fp=open(openfilename,"r") 0205 fp.close() 0206 except: 0207 print "Could not open File: " 0208 print sys.exc_info()[1] 0209 0210 print "open", openfilename.encode(enc) 0211 0212 # dialog for saving files 0213 0214 saveasfilename=asksaveasfilename() 0215 print "saveas", saveasfilename.encode(enc) 0216
Generated by PyXR 0.9.4