FileDialog.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. """File selection dialog classes.
  2. Classes:
  3. - FileDialog
  4. - LoadFileDialog
  5. - SaveFileDialog
  6. """
  7. from Tkinter import *
  8. from Dialog import Dialog
  9. import os
  10. import fnmatch
  11. dialogstates = {}
  12. class FileDialog:
  13. """Standard file selection dialog -- no checks on selected file.
  14. Usage:
  15. d = FileDialog(master)
  16. fname = d.go(dir_or_file, pattern, default, key)
  17. if fname is None: ...canceled...
  18. else: ...open file...
  19. All arguments to go() are optional.
  20. The 'key' argument specifies a key in the global dictionary
  21. 'dialogstates', which keeps track of the values for the directory
  22. and pattern arguments, overriding the values passed in (it does
  23. not keep track of the default argument!). If no key is specified,
  24. the dialog keeps no memory of previous state. Note that memory is
  25. kept even when the dialog is canceled. (All this emulates the
  26. behavior of the Macintosh file selection dialogs.)
  27. """
  28. title = "File Selection Dialog"
  29. def __init__(self, master, title=None):
  30. if title is None: title = self.title
  31. self.master = master
  32. self.directory = None
  33. self.top = Toplevel(master)
  34. self.top.title(title)
  35. self.top.iconname(title)
  36. self.botframe = Frame(self.top)
  37. self.botframe.pack(side=BOTTOM, fill=X)
  38. self.selection = Entry(self.top)
  39. self.selection.pack(side=BOTTOM, fill=X)
  40. self.selection.bind('<Return>', self.ok_event)
  41. self.filter = Entry(self.top)
  42. self.filter.pack(side=TOP, fill=X)
  43. self.filter.bind('<Return>', self.filter_command)
  44. self.midframe = Frame(self.top)
  45. self.midframe.pack(expand=YES, fill=BOTH)
  46. self.filesbar = Scrollbar(self.midframe)
  47. self.filesbar.pack(side=RIGHT, fill=Y)
  48. self.files = Listbox(self.midframe, exportselection=0,
  49. yscrollcommand=(self.filesbar, 'set'))
  50. self.files.pack(side=RIGHT, expand=YES, fill=BOTH)
  51. btags = self.files.bindtags()
  52. self.files.bindtags(btags[1:] + btags[:1])
  53. self.files.bind('<ButtonRelease-1>', self.files_select_event)
  54. self.files.bind('<Double-ButtonRelease-1>', self.files_double_event)
  55. self.filesbar.config(command=(self.files, 'yview'))
  56. self.dirsbar = Scrollbar(self.midframe)
  57. self.dirsbar.pack(side=LEFT, fill=Y)
  58. self.dirs = Listbox(self.midframe, exportselection=0,
  59. yscrollcommand=(self.dirsbar, 'set'))
  60. self.dirs.pack(side=LEFT, expand=YES, fill=BOTH)
  61. self.dirsbar.config(command=(self.dirs, 'yview'))
  62. btags = self.dirs.bindtags()
  63. self.dirs.bindtags(btags[1:] + btags[:1])
  64. self.dirs.bind('<ButtonRelease-1>', self.dirs_select_event)
  65. self.dirs.bind('<Double-ButtonRelease-1>', self.dirs_double_event)
  66. self.ok_button = Button(self.botframe,
  67. text="OK",
  68. command=self.ok_command)
  69. self.ok_button.pack(side=LEFT)
  70. self.filter_button = Button(self.botframe,
  71. text="Filter",
  72. command=self.filter_command)
  73. self.filter_button.pack(side=LEFT, expand=YES)
  74. self.cancel_button = Button(self.botframe,
  75. text="Cancel",
  76. command=self.cancel_command)
  77. self.cancel_button.pack(side=RIGHT)
  78. self.top.protocol('WM_DELETE_WINDOW', self.cancel_command)
  79. # XXX Are the following okay for a general audience?
  80. self.top.bind('<Alt-w>', self.cancel_command)
  81. self.top.bind('<Alt-W>', self.cancel_command)
  82. def go(self, dir_or_file=os.curdir, pattern="*", default="", key=None):
  83. if key and key in dialogstates:
  84. self.directory, pattern = dialogstates[key]
  85. else:
  86. dir_or_file = os.path.expanduser(dir_or_file)
  87. if os.path.isdir(dir_or_file):
  88. self.directory = dir_or_file
  89. else:
  90. self.directory, default = os.path.split(dir_or_file)
  91. self.set_filter(self.directory, pattern)
  92. self.set_selection(default)
  93. self.filter_command()
  94. self.selection.focus_set()
  95. self.top.wait_visibility() # window needs to be visible for the grab
  96. self.top.grab_set()
  97. self.how = None
  98. self.master.mainloop() # Exited by self.quit(how)
  99. if key:
  100. directory, pattern = self.get_filter()
  101. if self.how:
  102. directory = os.path.dirname(self.how)
  103. dialogstates[key] = directory, pattern
  104. self.top.destroy()
  105. return self.how
  106. def quit(self, how=None):
  107. self.how = how
  108. self.master.quit() # Exit mainloop()
  109. def dirs_double_event(self, event):
  110. self.filter_command()
  111. def dirs_select_event(self, event):
  112. dir, pat = self.get_filter()
  113. subdir = self.dirs.get('active')
  114. dir = os.path.normpath(os.path.join(self.directory, subdir))
  115. self.set_filter(dir, pat)
  116. def files_double_event(self, event):
  117. self.ok_command()
  118. def files_select_event(self, event):
  119. file = self.files.get('active')
  120. self.set_selection(file)
  121. def ok_event(self, event):
  122. self.ok_command()
  123. def ok_command(self):
  124. self.quit(self.get_selection())
  125. def filter_command(self, event=None):
  126. dir, pat = self.get_filter()
  127. try:
  128. names = os.listdir(dir)
  129. except os.error:
  130. self.master.bell()
  131. return
  132. self.directory = dir
  133. self.set_filter(dir, pat)
  134. names.sort()
  135. subdirs = [os.pardir]
  136. matchingfiles = []
  137. for name in names:
  138. fullname = os.path.join(dir, name)
  139. if os.path.isdir(fullname):
  140. subdirs.append(name)
  141. elif fnmatch.fnmatch(name, pat):
  142. matchingfiles.append(name)
  143. self.dirs.delete(0, END)
  144. for name in subdirs:
  145. self.dirs.insert(END, name)
  146. self.files.delete(0, END)
  147. for name in matchingfiles:
  148. self.files.insert(END, name)
  149. head, tail = os.path.split(self.get_selection())
  150. if tail == os.curdir: tail = ''
  151. self.set_selection(tail)
  152. def get_filter(self):
  153. filter = self.filter.get()
  154. filter = os.path.expanduser(filter)
  155. if filter[-1:] == os.sep or os.path.isdir(filter):
  156. filter = os.path.join(filter, "*")
  157. return os.path.split(filter)
  158. def get_selection(self):
  159. file = self.selection.get()
  160. file = os.path.expanduser(file)
  161. return file
  162. def cancel_command(self, event=None):
  163. self.quit()
  164. def set_filter(self, dir, pat):
  165. if not os.path.isabs(dir):
  166. try:
  167. pwd = os.getcwd()
  168. except os.error:
  169. pwd = None
  170. if pwd:
  171. dir = os.path.join(pwd, dir)
  172. dir = os.path.normpath(dir)
  173. self.filter.delete(0, END)
  174. self.filter.insert(END, os.path.join(dir or os.curdir, pat or "*"))
  175. def set_selection(self, file):
  176. self.selection.delete(0, END)
  177. self.selection.insert(END, os.path.join(self.directory, file))
  178. class LoadFileDialog(FileDialog):
  179. """File selection dialog which checks that the file exists."""
  180. title = "Load File Selection Dialog"
  181. def ok_command(self):
  182. file = self.get_selection()
  183. if not os.path.isfile(file):
  184. self.master.bell()
  185. else:
  186. self.quit(file)
  187. class SaveFileDialog(FileDialog):
  188. """File selection dialog which checks that the file may be created."""
  189. title = "Save File Selection Dialog"
  190. def ok_command(self):
  191. file = self.get_selection()
  192. if os.path.exists(file):
  193. if os.path.isdir(file):
  194. self.master.bell()
  195. return
  196. d = Dialog(self.top,
  197. title="Overwrite Existing File Question",
  198. text="Overwrite existing file %r?" % (file,),
  199. bitmap='questhead',
  200. default=1,
  201. strings=("Yes", "Cancel"))
  202. if d.num != 0:
  203. return
  204. else:
  205. head, tail = os.path.split(file)
  206. if not os.path.isdir(head):
  207. self.master.bell()
  208. return
  209. self.quit(file)
  210. def test():
  211. """Simple test program."""
  212. root = Tk()
  213. root.withdraw()
  214. fd = LoadFileDialog(root)
  215. loadfile = fd.go(key="test")
  216. fd = SaveFileDialog(root)
  217. savefile = fd.go(key="test")
  218. print loadfile, savefile
  219. if __name__ == '__main__':
  220. test()