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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3031
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """File selection dialog classes.
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 Classes:
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 - FileDialog
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 - LoadFileDialog
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 - SaveFileDialog
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 """
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 from Tkinter import *
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 from Dialog import Dialog
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 import os
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 import fnmatch
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
17
3643
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
18 dialogstates = {}
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
19
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
20
3031
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 class FileDialog:
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 """Standard file selection dialog -- no checks on selected file.
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 Usage:
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 d = FileDialog(master)
3643
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
28 file = d.go(dir_or_file, pattern, default, key)
3031
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
31
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
32 All arguments to go() are optional.
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
33
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
34 The 'key' argument specifies a key in the global dictionary
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
35 'dialogstates', which keeps track of the values for the directory
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
36 and pattern arguments, overriding the values passed in (it does
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
37 not keep track of the default argument!). If no key is specified,
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
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
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
40 behavior of the Macintosh file selection dialogs.)
3031
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 """
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 title = "File Selection Dialog"
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45
3643
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
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
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
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
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
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
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
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
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
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
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
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
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
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
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
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
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
108
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
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
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
132
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
136
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
139
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
145
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
148
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
152
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
155
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
158
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
186
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
193
3643
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
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
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
198
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
201
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
213
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
217
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
218
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 class LoadFileDialog(FileDialog):
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
220
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 """File selection dialog which checks that the file exists."""
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
222
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 title = "Load File Selection Dialog"
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
224
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
231
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
232
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 class SaveFileDialog(FileDialog):
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
234
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 """File selection dialog which checks that the file may be created."""
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
236
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 title = "Save File Selection Dialog"
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
238
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
259
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
260
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 def test():
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 """Simple test program."""
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 root = Tk()
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 root.withdraw()
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 fd = LoadFileDialog(root)
3643
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
266 loadfile = fd.go(key="test")
3031
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 fd = SaveFileDialog(root)
3643
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
268 savefile = fd.go(key="test")
3031
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 print loadfile, savefile
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
270
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
271
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 if __name__ == '__main__':
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 test()