Mercurial > cpython-lru_cache-weakref
annotate Lib/lib-tk/FileDialog.py @ 39042:c02fa187e0f4 legacy-trunk
Part of SF patch #1484695. This removes dead code. The chksum was
already verified in .frombuf() on the lines above. If there was
a problem an exception is raised, so there was no way this condition
could have been true.
| author | Neal Norwitz <nnorwitz@gmail.com> |
|---|---|
| date | Mon, 10 Jul 2006 00:23:17 +0000 |
| parents | 22ded309648b |
| children | 0f135affabf2 |
| 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 import os | |
| 15 import fnmatch | |
| 16 | |
| 17 | |
| 3643 | 18 dialogstates = {} |
| 19 | |
| 20 | |
| 3031 | 21 class FileDialog: |
| 22 | |
| 23 """Standard file selection dialog -- no checks on selected file. | |
| 24 | |
| 25 Usage: | |
| 26 | |
| 27 d = FileDialog(master) | |
|
27325
c404879cf999
Fix example in a docstring to not use 'file' as a variable name
Andrew M. Kuchling <amk@amk.ca>
parents:
13620
diff
changeset
|
28 fname = d.go(dir_or_file, pattern, default, key) |
|
c404879cf999
Fix example in a docstring to not use 'file' as a variable name
Andrew M. Kuchling <amk@amk.ca>
parents:
13620
diff
changeset
|
29 if fname is None: ...canceled... |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
30 else: ...open file... |
| 3643 | 31 |
| 32 All arguments to go() are optional. | |
| 33 | |
| 34 The 'key' argument specifies a key in the global dictionary | |
| 35 'dialogstates', which keeps track of the values for the directory | |
| 36 and pattern arguments, overriding the values passed in (it does | |
| 37 not keep track of the default argument!). If no key is specified, | |
| 38 the dialog keeps no memory of previous state. Note that memory is | |
|
13620
5b727ab37092
Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents:
8227
diff
changeset
|
39 kept even when the dialog is canceled. (All this emulates the |
| 3643 | 40 behavior of the Macintosh file selection dialogs.) |
| 3031 | 41 |
| 42 """ | |
| 43 | |
| 44 title = "File Selection Dialog" | |
| 45 | |
| 3643 | 46 def __init__(self, master, title=None): |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
47 if title is None: title = self.title |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
48 self.master = master |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
49 self.directory = None |
| 3643 | 50 |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
51 self.top = Toplevel(master) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
52 self.top.title(title) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
53 self.top.iconname(title) |
| 3643 | 54 |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
55 self.botframe = Frame(self.top) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
56 self.botframe.pack(side=BOTTOM, fill=X) |
| 3643 | 57 |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
58 self.selection = Entry(self.top) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
59 self.selection.pack(side=BOTTOM, fill=X) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
60 self.selection.bind('<Return>', self.ok_event) |
| 3643 | 61 |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
62 self.filter = Entry(self.top) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
63 self.filter.pack(side=TOP, fill=X) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
64 self.filter.bind('<Return>', self.filter_command) |
| 3643 | 65 |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
66 self.midframe = Frame(self.top) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
67 self.midframe.pack(expand=YES, fill=BOTH) |
| 3643 | 68 |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
69 self.filesbar = Scrollbar(self.midframe) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
70 self.filesbar.pack(side=RIGHT, fill=Y) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
71 self.files = Listbox(self.midframe, exportselection=0, |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
72 yscrollcommand=(self.filesbar, 'set')) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
73 self.files.pack(side=RIGHT, expand=YES, fill=BOTH) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
74 btags = self.files.bindtags() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
75 self.files.bindtags(btags[1:] + btags[:1]) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
76 self.files.bind('<ButtonRelease-1>', self.files_select_event) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
77 self.files.bind('<Double-ButtonRelease-1>', self.files_double_event) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
78 self.filesbar.config(command=(self.files, 'yview')) |
| 3643 | 79 |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
80 self.dirsbar = Scrollbar(self.midframe) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
81 self.dirsbar.pack(side=LEFT, fill=Y) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
82 self.dirs = Listbox(self.midframe, exportselection=0, |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
83 yscrollcommand=(self.dirsbar, 'set')) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
84 self.dirs.pack(side=LEFT, expand=YES, fill=BOTH) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
85 self.dirsbar.config(command=(self.dirs, 'yview')) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
86 btags = self.dirs.bindtags() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
87 self.dirs.bindtags(btags[1:] + btags[:1]) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
88 self.dirs.bind('<ButtonRelease-1>', self.dirs_select_event) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
89 self.dirs.bind('<Double-ButtonRelease-1>', self.dirs_double_event) |
| 3643 | 90 |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
91 self.ok_button = Button(self.botframe, |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
92 text="OK", |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
93 command=self.ok_command) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
94 self.ok_button.pack(side=LEFT) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
95 self.filter_button = Button(self.botframe, |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
96 text="Filter", |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
97 command=self.filter_command) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
98 self.filter_button.pack(side=LEFT, expand=YES) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
99 self.cancel_button = Button(self.botframe, |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
100 text="Cancel", |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
101 command=self.cancel_command) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
102 self.cancel_button.pack(side=RIGHT) |
| 3031 | 103 |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
104 self.top.protocol('WM_DELETE_WINDOW', self.cancel_command) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
105 # XXX Are the following okay for a general audience? |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
106 self.top.bind('<Alt-w>', self.cancel_command) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
107 self.top.bind('<Alt-W>', self.cancel_command) |
| 3643 | 108 |
| 109 def go(self, dir_or_file=os.curdir, pattern="*", default="", key=None): | |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
110 if key and dialogstates.has_key(key): |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
111 self.directory, pattern = dialogstates[key] |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
112 else: |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
113 dir_or_file = os.path.expanduser(dir_or_file) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
114 if os.path.isdir(dir_or_file): |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
115 self.directory = dir_or_file |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
116 else: |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
117 self.directory, default = os.path.split(dir_or_file) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
118 self.set_filter(self.directory, pattern) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
119 self.set_selection(default) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
120 self.filter_command() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
121 self.selection.focus_set() |
|
32940
22ded309648b
Wait for visibility before setting a grab. Fixes #946153.
Martin v. Löwis <martin@v.loewis.de>
parents:
31515
diff
changeset
|
122 self.top.wait_visibility() # window needs to be visible for the grab |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
123 self.top.grab_set() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
124 self.how = None |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
125 self.master.mainloop() # Exited by self.quit(how) |
|
8227
4a13a4d2588b
When a file name is selected ("OK" button, <Return> in the filename entry),
Fred Drake <fdrake@acm.org>
parents:
7731
diff
changeset
|
126 if key: |
|
4a13a4d2588b
When a file name is selected ("OK" button, <Return> in the filename entry),
Fred Drake <fdrake@acm.org>
parents:
7731
diff
changeset
|
127 directory, pattern = self.get_filter() |
|
4a13a4d2588b
When a file name is selected ("OK" button, <Return> in the filename entry),
Fred Drake <fdrake@acm.org>
parents:
7731
diff
changeset
|
128 if self.how: |
|
4a13a4d2588b
When a file name is selected ("OK" button, <Return> in the filename entry),
Fred Drake <fdrake@acm.org>
parents:
7731
diff
changeset
|
129 directory = os.path.dirname(self.how) |
|
4a13a4d2588b
When a file name is selected ("OK" button, <Return> in the filename entry),
Fred Drake <fdrake@acm.org>
parents:
7731
diff
changeset
|
130 dialogstates[key] = directory, pattern |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
131 self.top.destroy() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
132 return self.how |
| 3643 | 133 |
| 134 def quit(self, how=None): | |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
135 self.how = how |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
136 self.master.quit() # Exit mainloop() |
| 3031 | 137 |
| 138 def dirs_double_event(self, event): | |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
139 self.filter_command() |
| 3031 | 140 |
| 141 def dirs_select_event(self, event): | |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
142 dir, pat = self.get_filter() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
143 subdir = self.dirs.get('active') |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
144 dir = os.path.normpath(os.path.join(self.directory, subdir)) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
145 self.set_filter(dir, pat) |
| 3031 | 146 |
| 147 def files_double_event(self, event): | |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
148 self.ok_command() |
| 3031 | 149 |
| 150 def files_select_event(self, event): | |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
151 file = self.files.get('active') |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
152 self.set_selection(file) |
| 3031 | 153 |
| 154 def ok_event(self, event): | |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
155 self.ok_command() |
| 3031 | 156 |
| 157 def ok_command(self): | |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
158 self.quit(self.get_selection()) |
| 3031 | 159 |
| 160 def filter_command(self, event=None): | |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
161 dir, pat = self.get_filter() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
162 try: |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
163 names = os.listdir(dir) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
164 except os.error: |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
165 self.master.bell() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
166 return |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
167 self.directory = dir |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
168 self.set_filter(dir, pat) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
169 names.sort() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
170 subdirs = [os.pardir] |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
171 matchingfiles = [] |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
172 for name in names: |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
173 fullname = os.path.join(dir, name) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
174 if os.path.isdir(fullname): |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
175 subdirs.append(name) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
176 elif fnmatch.fnmatch(name, pat): |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
177 matchingfiles.append(name) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
178 self.dirs.delete(0, END) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
179 for name in subdirs: |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
180 self.dirs.insert(END, name) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
181 self.files.delete(0, END) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
182 for name in matchingfiles: |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
183 self.files.insert(END, name) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
184 head, tail = os.path.split(self.get_selection()) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
185 if tail == os.curdir: tail = '' |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
186 self.set_selection(tail) |
| 3031 | 187 |
| 188 def get_filter(self): | |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
189 filter = self.filter.get() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
190 filter = os.path.expanduser(filter) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
191 if filter[-1:] == os.sep or os.path.isdir(filter): |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
192 filter = os.path.join(filter, "*") |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
193 return os.path.split(filter) |
| 3031 | 194 |
| 3643 | 195 def get_selection(self): |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
196 file = self.selection.get() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
197 file = os.path.expanduser(file) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
198 return file |
| 3643 | 199 |
| 200 def cancel_command(self, event=None): | |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
201 self.quit() |
| 3031 | 202 |
| 203 def set_filter(self, dir, pat): | |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
204 if not os.path.isabs(dir): |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
205 try: |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
206 pwd = os.getcwd() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
207 except os.error: |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
208 pwd = None |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
209 if pwd: |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
210 dir = os.path.join(pwd, dir) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
211 dir = os.path.normpath(dir) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
212 self.filter.delete(0, END) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
213 self.filter.insert(END, os.path.join(dir or os.curdir, pat or "*")) |
| 3031 | 214 |
| 215 def set_selection(self, file): | |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
216 self.selection.delete(0, END) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
217 self.selection.insert(END, os.path.join(self.directory, file)) |
| 3031 | 218 |
| 219 | |
| 220 class LoadFileDialog(FileDialog): | |
| 221 | |
| 222 """File selection dialog which checks that the file exists.""" | |
| 223 | |
| 224 title = "Load File Selection Dialog" | |
| 225 | |
| 226 def ok_command(self): | |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
227 file = self.get_selection() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
228 if not os.path.isfile(file): |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
229 self.master.bell() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
230 else: |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
231 self.quit(file) |
| 3031 | 232 |
| 233 | |
| 234 class SaveFileDialog(FileDialog): | |
| 235 | |
| 236 """File selection dialog which checks that the file may be created.""" | |
| 237 | |
| 238 title = "Save File Selection Dialog" | |
| 239 | |
| 240 def ok_command(self): | |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
241 file = self.get_selection() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
242 if os.path.exists(file): |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
243 if os.path.isdir(file): |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
244 self.master.bell() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
245 return |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
246 d = Dialog(self.top, |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
247 title="Overwrite Existing File Question", |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
27325
diff
changeset
|
248 text="Overwrite existing file %r?" % (file,), |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
249 bitmap='questhead', |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
250 default=1, |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
251 strings=("Yes", "Cancel")) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
252 if d.num != 0: |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
253 return |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
254 else: |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
255 head, tail = os.path.split(file) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
256 if not os.path.isdir(head): |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
257 self.master.bell() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
258 return |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
259 self.quit(file) |
| 3031 | 260 |
| 261 | |
| 262 def test(): | |
| 263 """Simple test program.""" | |
| 264 root = Tk() | |
| 265 root.withdraw() | |
| 266 fd = LoadFileDialog(root) | |
| 3643 | 267 loadfile = fd.go(key="test") |
| 3031 | 268 fd = SaveFileDialog(root) |
| 3643 | 269 savefile = fd.go(key="test") |
| 3031 | 270 print loadfile, savefile |
| 271 | |
| 272 | |
| 273 if __name__ == '__main__': | |
| 274 test() |
