filedialog.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. """File selection dialog classes.
  2. Classes:
  3. - FileDialog
  4. - LoadFileDialog
  5. - SaveFileDialog
  6. This module also presents tk common file dialogues, it provides interfaces
  7. to the native file dialogues available in Tk 4.2 and newer, and the
  8. directory dialogue available in Tk 8.3 and newer.
  9. These interfaces were written by Fredrik Lundh, May 1997.
  10. """
  11. __all__ = ["FileDialog", "LoadFileDialog", "SaveFileDialog",
  12. "Open", "SaveAs", "Directory",
  13. "askopenfilename", "asksaveasfilename", "askopenfilenames",
  14. "askopenfile", "askopenfiles", "asksaveasfile", "askdirectory"]
  15. import fnmatch
  16. import os
  17. from tkinter import (
  18. Frame, LEFT, YES, BOTTOM, Entry, TOP, Button, Tk, X,
  19. Toplevel, RIGHT, Y, END, Listbox, BOTH, Scrollbar,
  20. )
  21. from tkinter.dialog import Dialog
  22. from tkinter import commondialog
  23. from tkinter.simpledialog import _setup_dialog
  24. dialogstates = {}
  25. class FileDialog:
  26. """Standard file selection dialog -- no checks on selected file.
  27. Usage:
  28. d = FileDialog(master)
  29. fname = d.go(dir_or_file, pattern, default, key)
  30. if fname is None: ...canceled...
  31. else: ...open file...
  32. All arguments to go() are optional.
  33. The 'key' argument specifies a key in the global dictionary
  34. 'dialogstates', which keeps track of the values for the directory
  35. and pattern arguments, overriding the values passed in (it does
  36. not keep track of the default argument!). If no key is specified,
  37. the dialog keeps no memory of previous state. Note that memory is
  38. kept even when the dialog is canceled. (All this emulates the
  39. behavior of the Macintosh file selection dialogs.)
  40. """
  41. title = "File Selection Dialog"
  42. def __init__(self, master, title=None):
  43. if title is None: title = self.title
  44. self.master = master
  45. self.directory = None
  46. self.top = Toplevel(master)
  47. self.top.title(title)
  48. self.top.iconname(title)
  49. _setup_dialog(self.top)
  50. self.botframe = Frame(self.top)
  51. self.botframe.pack(side=BOTTOM, fill=X)
  52. self.selection = Entry(self.top)
  53. self.selection.pack(side=BOTTOM, fill=X)
  54. self.selection.bind('<Return>', self.ok_event)
  55. self.filter = Entry(self.top)
  56. self.filter.pack(side=TOP, fill=X)
  57. self.filter.bind('<Return>', self.filter_command)
  58. self.midframe = Frame(self.top)
  59. self.midframe.pack(expand=YES, fill=BOTH)
  60. self.filesbar = Scrollbar(self.midframe)
  61. self.filesbar.pack(side=RIGHT, fill=Y)
  62. self.files = Listbox(self.midframe, exportselection=0,
  63. yscrollcommand=(self.filesbar, 'set'))
  64. self.files.pack(side=RIGHT, expand=YES, fill=BOTH)
  65. btags = self.files.bindtags()
  66. self.files.bindtags(btags[1:] + btags[:1])
  67. self.files.bind('<ButtonRelease-1>', self.files_select_event)
  68. self.files.bind('<Double-ButtonRelease-1>', self.files_double_event)
  69. self.filesbar.config(command=(self.files, 'yview'))
  70. self.dirsbar = Scrollbar(self.midframe)
  71. self.dirsbar.pack(side=LEFT, fill=Y)
  72. self.dirs = Listbox(self.midframe, exportselection=0,
  73. yscrollcommand=(self.dirsbar, 'set'))
  74. self.dirs.pack(side=LEFT, expand=YES, fill=BOTH)
  75. self.dirsbar.config(command=(self.dirs, 'yview'))
  76. btags = self.dirs.bindtags()
  77. self.dirs.bindtags(btags[1:] + btags[:1])
  78. self.dirs.bind('<ButtonRelease-1>', self.dirs_select_event)
  79. self.dirs.bind('<Double-ButtonRelease-1>', self.dirs_double_event)
  80. self.ok_button = Button(self.botframe,
  81. text="OK",
  82. command=self.ok_command)
  83. self.ok_button.pack(side=LEFT)
  84. self.filter_button = Button(self.botframe,
  85. text="Filter",
  86. command=self.filter_command)
  87. self.filter_button.pack(side=LEFT, expand=YES)
  88. self.cancel_button = Button(self.botframe,
  89. text="Cancel",
  90. command=self.cancel_command)
  91. self.cancel_button.pack(side=RIGHT)
  92. self.top.protocol('WM_DELETE_WINDOW', self.cancel_command)
  93. # XXX Are the following okay for a general audience?
  94. self.top.bind('<Alt-w>', self.cancel_command)
  95. self.top.bind('<Alt-W>', self.cancel_command)
  96. def go(self, dir_or_file=os.curdir, pattern="*", default="", key=None):
  97. if key and key in dialogstates:
  98. self.directory, pattern = dialogstates[key]
  99. else:
  100. dir_or_file = os.path.expanduser(dir_or_file)
  101. if os.path.isdir(dir_or_file):
  102. self.directory = dir_or_file
  103. else:
  104. self.directory, default = os.path.split(dir_or_file)
  105. self.set_filter(self.directory, pattern)
  106. self.set_selection(default)
  107. self.filter_command()
  108. self.selection.focus_set()
  109. self.top.wait_visibility() # window needs to be visible for the grab
  110. self.top.grab_set()
  111. self.how = None
  112. self.master.mainloop() # Exited by self.quit(how)
  113. if key:
  114. directory, pattern = self.get_filter()
  115. if self.how:
  116. directory = os.path.dirname(self.how)
  117. dialogstates[key] = directory, pattern
  118. self.top.destroy()
  119. return self.how
  120. def quit(self, how=None):
  121. self.how = how
  122. self.master.quit() # Exit mainloop()
  123. def dirs_double_event(self, event):
  124. self.filter_command()
  125. def dirs_select_event(self, event):
  126. dir, pat = self.get_filter()
  127. subdir = self.dirs.get('active')
  128. dir = os.path.normpath(os.path.join(self.directory, subdir))
  129. self.set_filter(dir, pat)
  130. def files_double_event(self, event):
  131. self.ok_command()
  132. def files_select_event(self, event):
  133. file = self.files.get('active')
  134. self.set_selection(file)
  135. def ok_event(self, event):
  136. self.ok_command()
  137. def ok_command(self):
  138. self.quit(self.get_selection())
  139. def filter_command(self, event=None):
  140. dir, pat = self.get_filter()
  141. try:
  142. names = os.listdir(dir)
  143. except OSError:
  144. self.master.bell()
  145. return
  146. self.directory = dir
  147. self.set_filter(dir, pat)
  148. names.sort()
  149. subdirs = [os.pardir]
  150. matchingfiles = []
  151. for name in names:
  152. fullname = os.path.join(dir, name)
  153. if os.path.isdir(fullname):
  154. subdirs.append(name)
  155. elif fnmatch.fnmatch(name, pat):
  156. matchingfiles.append(name)
  157. self.dirs.delete(0, END)
  158. for name in subdirs:
  159. self.dirs.insert(END, name)
  160. self.files.delete(0, END)
  161. for name in matchingfiles:
  162. self.files.insert(END, name)
  163. head, tail = os.path.split(self.get_selection())
  164. if tail == os.curdir: tail = ''
  165. self.set_selection(tail)
  166. def get_filter(self):
  167. filter = self.filter.get()
  168. filter = os.path.expanduser(filter)
  169. if filter[-1:] == os.sep or os.path.isdir(filter):
  170. filter = os.path.join(filter, "*")
  171. return os.path.split(filter)
  172. def get_selection(self):
  173. file = self.selection.get()
  174. file = os.path.expanduser(file)
  175. return file
  176. def cancel_command(self, event=None):
  177. self.quit()
  178. def set_filter(self, dir, pat):
  179. if not os.path.isabs(dir):
  180. try:
  181. pwd = os.getcwd()
  182. except OSError:
  183. pwd = None
  184. if pwd:
  185. dir = os.path.join(pwd, dir)
  186. dir = os.path.normpath(dir)
  187. self.filter.delete(0, END)
  188. self.filter.insert(END, os.path.join(dir or os.curdir, pat or "*"))
  189. def set_selection(self, file):
  190. self.selection.delete(0, END)
  191. self.selection.insert(END, os.path.join(self.directory, file))
  192. class LoadFileDialog(FileDialog):
  193. """File selection dialog which checks that the file exists."""
  194. title = "Load File Selection Dialog"
  195. def ok_command(self):
  196. file = self.get_selection()
  197. if not os.path.isfile(file):
  198. self.master.bell()
  199. else:
  200. self.quit(file)
  201. class SaveFileDialog(FileDialog):
  202. """File selection dialog which checks that the file may be created."""
  203. title = "Save File Selection Dialog"
  204. def ok_command(self):
  205. file = self.get_selection()
  206. if os.path.exists(file):
  207. if os.path.isdir(file):
  208. self.master.bell()
  209. return
  210. d = Dialog(self.top,
  211. title="Overwrite Existing File Question",
  212. text="Overwrite existing file %r?" % (file,),
  213. bitmap='questhead',
  214. default=1,
  215. strings=("Yes", "Cancel"))
  216. if d.num != 0:
  217. return
  218. else:
  219. head, tail = os.path.split(file)
  220. if not os.path.isdir(head):
  221. self.master.bell()
  222. return
  223. self.quit(file)
  224. # For the following classes and modules:
  225. #
  226. # options (all have default values):
  227. #
  228. # - defaultextension: added to filename if not explicitly given
  229. #
  230. # - filetypes: sequence of (label, pattern) tuples. the same pattern
  231. # may occur with several patterns. use "*" as pattern to indicate
  232. # all files.
  233. #
  234. # - initialdir: initial directory. preserved by dialog instance.
  235. #
  236. # - initialfile: initial file (ignored by the open dialog). preserved
  237. # by dialog instance.
  238. #
  239. # - parent: which window to place the dialog on top of
  240. #
  241. # - title: dialog title
  242. #
  243. # - multiple: if true user may select more than one file
  244. #
  245. # options for the directory chooser:
  246. #
  247. # - initialdir, parent, title: see above
  248. #
  249. # - mustexist: if true, user must pick an existing directory
  250. #
  251. class _Dialog(commondialog.Dialog):
  252. def _fixoptions(self):
  253. try:
  254. # make sure "filetypes" is a tuple
  255. self.options["filetypes"] = tuple(self.options["filetypes"])
  256. except KeyError:
  257. pass
  258. def _fixresult(self, widget, result):
  259. if result:
  260. # keep directory and filename until next time
  261. # convert Tcl path objects to strings
  262. try:
  263. result = result.string
  264. except AttributeError:
  265. # it already is a string
  266. pass
  267. path, file = os.path.split(result)
  268. self.options["initialdir"] = path
  269. self.options["initialfile"] = file
  270. self.filename = result # compatibility
  271. return result
  272. #
  273. # file dialogs
  274. class Open(_Dialog):
  275. "Ask for a filename to open"
  276. command = "tk_getOpenFile"
  277. def _fixresult(self, widget, result):
  278. if isinstance(result, tuple):
  279. # multiple results:
  280. result = tuple([getattr(r, "string", r) for r in result])
  281. if result:
  282. path, file = os.path.split(result[0])
  283. self.options["initialdir"] = path
  284. # don't set initialfile or filename, as we have multiple of these
  285. return result
  286. if not widget.tk.wantobjects() and "multiple" in self.options:
  287. # Need to split result explicitly
  288. return self._fixresult(widget, widget.tk.splitlist(result))
  289. return _Dialog._fixresult(self, widget, result)
  290. class SaveAs(_Dialog):
  291. "Ask for a filename to save as"
  292. command = "tk_getSaveFile"
  293. # the directory dialog has its own _fix routines.
  294. class Directory(commondialog.Dialog):
  295. "Ask for a directory"
  296. command = "tk_chooseDirectory"
  297. def _fixresult(self, widget, result):
  298. if result:
  299. # convert Tcl path objects to strings
  300. try:
  301. result = result.string
  302. except AttributeError:
  303. # it already is a string
  304. pass
  305. # keep directory until next time
  306. self.options["initialdir"] = result
  307. self.directory = result # compatibility
  308. return result
  309. #
  310. # convenience stuff
  311. def askopenfilename(**options):
  312. "Ask for a filename to open"
  313. return Open(**options).show()
  314. def asksaveasfilename(**options):
  315. "Ask for a filename to save as"
  316. return SaveAs(**options).show()
  317. def askopenfilenames(**options):
  318. """Ask for multiple filenames to open
  319. Returns a list of filenames or empty list if
  320. cancel button selected
  321. """
  322. options["multiple"]=1
  323. return Open(**options).show()
  324. # FIXME: are the following perhaps a bit too convenient?
  325. def askopenfile(mode = "r", **options):
  326. "Ask for a filename to open, and returned the opened file"
  327. filename = Open(**options).show()
  328. if filename:
  329. return open(filename, mode)
  330. return None
  331. def askopenfiles(mode = "r", **options):
  332. """Ask for multiple filenames and return the open file
  333. objects
  334. returns a list of open file objects or an empty list if
  335. cancel selected
  336. """
  337. files = askopenfilenames(**options)
  338. if files:
  339. ofiles=[]
  340. for filename in files:
  341. ofiles.append(open(filename, mode))
  342. files=ofiles
  343. return files
  344. def asksaveasfile(mode = "w", **options):
  345. "Ask for a filename to save as, and returned the opened file"
  346. filename = SaveAs(**options).show()
  347. if filename:
  348. return open(filename, mode)
  349. return None
  350. def askdirectory (**options):
  351. "Ask for a directory, and return the file name"
  352. return Directory(**options).show()
  353. # --------------------------------------------------------------------
  354. # test stuff
  355. def test():
  356. """Simple test program."""
  357. root = Tk()
  358. root.withdraw()
  359. fd = LoadFileDialog(root)
  360. loadfile = fd.go(key="test")
  361. fd = SaveFileDialog(root)
  362. savefile = fd.go(key="test")
  363. print(loadfile, savefile)
  364. # Since the file name may contain non-ASCII characters, we need
  365. # to find an encoding that likely supports the file name, and
  366. # displays correctly on the terminal.
  367. # Start off with UTF-8
  368. enc = "utf-8"
  369. import sys
  370. # See whether CODESET is defined
  371. try:
  372. import locale
  373. locale.setlocale(locale.LC_ALL,'')
  374. enc = locale.nl_langinfo(locale.CODESET)
  375. except (ImportError, AttributeError):
  376. pass
  377. # dialog for opening files
  378. openfilename=askopenfilename(filetypes=[("all files", "*")])
  379. try:
  380. fp=open(openfilename,"r")
  381. fp.close()
  382. except:
  383. print("Could not open File: ")
  384. print(sys.exc_info()[1])
  385. print("open", openfilename.encode(enc))
  386. # dialog for saving files
  387. saveasfilename=asksaveasfilename()
  388. print("saveas", saveasfilename.encode(enc))
  389. if __name__ == '__main__':
  390. test()