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