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