1from __future__ import print_function 2""" 3 4.. moduleauthor:: easygui developers and Stephen Raymond Ferg 5.. default-domain:: py 6.. highlight:: python 7 8Version |release| 9""" 10 11import os 12try: 13 from . import utils as ut 14 from . import fileboxsetup as fbs 15except (SystemError, ValueError, ImportError): 16 import utils as ut 17 import fileboxsetup as fbs 18 19tk = ut.tk 20 21 22 23# ------------------------------------------------------------------- 24# fileopenbox 25# ------------------------------------------------------------------- 26 27 28def fileopenbox(msg=None, title=None, default='*', filetypes=None, multiple=False): 29 """ 30 A dialog to get a file name. 31 32 **About the "default" argument** 33 34 The "default" argument specifies a filepath that (normally) 35 contains one or more wildcards. 36 fileopenbox will display only files that match the default filepath. 37 If omitted, defaults to "\*" (all files in the current directory). 38 39 WINDOWS EXAMPLE:: 40 41 ...default="c:/myjunk/*.py" 42 43 will open in directory c:\\myjunk\\ and show all Python files. 44 45 WINDOWS EXAMPLE:: 46 47 ...default="c:/myjunk/test*.py" 48 49 will open in directory c:\\myjunk\\ and show all Python files 50 whose names begin with "test". 51 52 53 Note that on Windows, fileopenbox automatically changes the path 54 separator to the Windows path separator (backslash). 55 56 **About the "filetypes" argument** 57 58 If specified, it should contain a list of items, 59 where each item is either: 60 61 - a string containing a filemask # e.g. "\*.txt" 62 - a list of strings, where all of the strings except the last one 63 are filemasks (each beginning with "\*.", 64 such as "\*.txt" for text files, "\*.py" for Python files, etc.). 65 and the last string contains a filetype description 66 67 EXAMPLE:: 68 69 filetypes = ["*.css", ["*.htm", "*.html", "HTML files"] ] 70 71 .. note:: If the filetypes list does not contain ("All files","*"), it will be added. 72 73 If the filetypes list does not contain a filemask that includes 74 the extension of the "default" argument, it will be added. 75 For example, if default="\*abc.py" 76 and no filetypes argument was specified, then 77 "\*.py" will automatically be added to the filetypes argument. 78 79 :param str msg: the msg to be displayed. 80 :param str title: the window title 81 :param str default: filepath with wildcards 82 :param object filetypes: filemasks that a user can choose, e.g. "\*.txt" 83 :param bool multiple: If true, more than one file can be selected 84 :return: the name of a file, or None if user chose to cancel 85 """ 86 localRoot = tk.Tk() 87 localRoot.withdraw() 88 89 initialbase, initialfile, initialdir, filetypes = fbs.fileboxSetup( 90 default, filetypes) 91 92 # ------------------------------------------------------------ 93 # if initialfile contains no wildcards; we don't want an 94 # initial file. It won't be used anyway. 95 # Also: if initialbase is simply "*", we don't want an 96 # initialfile; it is not doing any useful work. 97 # ------------------------------------------------------------ 98 if (initialfile.find("*") < 0) and (initialfile.find("?") < 0): 99 initialfile = None 100 elif initialbase == "*": 101 initialfile = None 102 103 func = ut.tk_FileDialog.askopenfilenames if multiple else ut.tk_FileDialog.askopenfilename 104 ret_val = func(parent=localRoot, 105 title=ut.getFileDialogTitle(msg, title), 106 initialdir=initialdir, initialfile=initialfile, 107 filetypes=filetypes 108 ) 109 if not ret_val or ret_val == '': 110 return None 111 if multiple: 112 f = [os.path.normpath(x) for x in localRoot.tk.splitlist(ret_val)] 113 else: 114 try: 115 f = os.path.normpath(ret_val) 116 except AttributeError as e: 117 print("ret_val is {}".format(ret_val)) 118 raise e 119 localRoot.destroy() 120 121 if not f: 122 return None 123 return f 124 125 126if __name__ == '__main__': 127 print("Hello from file open box") 128 ret_val = fileopenbox("Please select a file", "My File Open dialog") 129 print("Return value is:{}".format(ret_val)) 130