Mercurial > cpython-lru_cache-weakref
annotate Lib/lib-tk/FileDialog.py @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | 5b727ab37092 |
| children | c404879cf999 |
| 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) | |
| 3643 | 28 file = d.go(dir_or_file, pattern, default, key) |
| 3031 | 29 if file 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() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
122 self.top.grab_set() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
123 self.how = None |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
124 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
|
125 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
|
126 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
|
127 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
|
128 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
|
129 dialogstates[key] = directory, pattern |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
130 self.top.destroy() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
131 return self.how |
| 3643 | 132 |
| 133 def quit(self, how=None): | |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
134 self.how = how |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
135 self.master.quit() # Exit mainloop() |
| 3031 | 136 |
| 137 def dirs_double_event(self, event): | |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
138 self.filter_command() |
| 3031 | 139 |
| 140 def dirs_select_event(self, event): | |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
141 dir, pat = self.get_filter() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
142 subdir = self.dirs.get('active') |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
143 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
|
144 self.set_filter(dir, pat) |
| 3031 | 145 |
| 146 def files_double_event(self, event): | |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
147 self.ok_command() |
| 3031 | 148 |
| 149 def files_select_event(self, event): | |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
150 file = self.files.get('active') |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
151 self.set_selection(file) |
| 3031 | 152 |
| 153 def ok_event(self, event): | |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
154 self.ok_command() |
| 3031 | 155 |
| 156 def ok_command(self): | |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
157 self.quit(self.get_selection()) |
| 3031 | 158 |
| 159 def filter_command(self, event=None): | |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
160 dir, pat = self.get_filter() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
161 try: |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
162 names = os.listdir(dir) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
163 except os.error: |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
164 self.master.bell() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
165 return |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
166 self.directory = dir |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
167 self.set_filter(dir, pat) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
168 names.sort() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
169 subdirs = [os.pardir] |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
170 matchingfiles = [] |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
171 for name in names: |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
172 fullname = os.path.join(dir, name) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
173 if os.path.isdir(fullname): |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
174 subdirs.append(name) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
175 elif fnmatch.fnmatch(name, pat): |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
176 matchingfiles.append(name) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
177 self.dirs.delete(0, END) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
178 for name in subdirs: |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
179 self.dirs.insert(END, name) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
180 self.files.delete(0, END) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
181 for name in matchingfiles: |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
182 self.files.insert(END, name) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
183 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
|
184 if tail == os.curdir: tail = '' |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
185 self.set_selection(tail) |
| 3031 | 186 |
| 187 def get_filter(self): | |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
188 filter = self.filter.get() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
189 filter = os.path.expanduser(filter) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
190 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
|
191 filter = os.path.join(filter, "*") |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
192 return os.path.split(filter) |
| 3031 | 193 |
| 3643 | 194 def get_selection(self): |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
195 file = self.selection.get() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
196 file = os.path.expanduser(file) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
197 return file |
| 3643 | 198 |
| 199 def cancel_command(self, event=None): | |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
200 self.quit() |
| 3031 | 201 |
| 202 def set_filter(self, dir, pat): | |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
203 if not os.path.isabs(dir): |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
204 try: |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
205 pwd = os.getcwd() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
206 except os.error: |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
207 pwd = None |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
208 if pwd: |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
209 dir = os.path.join(pwd, dir) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
210 dir = os.path.normpath(dir) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
211 self.filter.delete(0, END) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
212 self.filter.insert(END, os.path.join(dir or os.curdir, pat or "*")) |
| 3031 | 213 |
| 214 def set_selection(self, file): | |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
215 self.selection.delete(0, END) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
216 self.selection.insert(END, os.path.join(self.directory, file)) |
| 3031 | 217 |
| 218 | |
| 219 class LoadFileDialog(FileDialog): | |
| 220 | |
| 221 """File selection dialog which checks that the file exists.""" | |
| 222 | |
| 223 title = "Load File Selection Dialog" | |
| 224 | |
| 225 def ok_command(self): | |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
226 file = self.get_selection() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
227 if not os.path.isfile(file): |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
228 self.master.bell() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
229 else: |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
230 self.quit(file) |
| 3031 | 231 |
| 232 | |
| 233 class SaveFileDialog(FileDialog): | |
| 234 | |
| 235 """File selection dialog which checks that the file may be created.""" | |
| 236 | |
| 237 title = "Save File Selection Dialog" | |
| 238 | |
| 239 def ok_command(self): | |
|
7731
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
240 file = self.get_selection() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
241 if os.path.exists(file): |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
242 if os.path.isdir(file): |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
243 self.master.bell() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
244 return |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
245 d = Dialog(self.top, |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
246 title="Overwrite Existing File Question", |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
247 text="Overwrite existing file %s?" % `file`, |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
248 bitmap='questhead', |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
249 default=1, |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
250 strings=("Yes", "Cancel")) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
251 if d.num != 0: |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
252 return |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
253 else: |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
254 head, tail = os.path.split(file) |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
255 if not os.path.isdir(head): |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
256 self.master.bell() |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
257 return |
|
9c37ed8868b9
Give in to the tab police.
Guido van Rossum <guido@python.org>
parents:
4893
diff
changeset
|
258 self.quit(file) |
| 3031 | 259 |
| 260 | |
| 261 def test(): | |
| 262 """Simple test program.""" | |
| 263 root = Tk() | |
| 264 root.withdraw() | |
| 265 fd = LoadFileDialog(root) | |
| 3643 | 266 loadfile = fd.go(key="test") |
| 3031 | 267 fd = SaveFileDialog(root) |
| 3643 | 268 savefile = fd.go(key="test") |
| 3031 | 269 print loadfile, savefile |
| 270 | |
| 271 | |
| 272 if __name__ == '__main__': | |
| 273 test() |
