annotate Lib/lib-tk/FileDialog.py @ 32940:22ded309648b legacy-trunk

Wait for visibility before setting a grab. Fixes #946153.
author Martin v. Löwis <martin@v.loewis.de>
date Tue, 03 Aug 2004 18:36:25 +0000
parents 71a294bd7950
children 0f135affabf2
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)
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
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()
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
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
133
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
137
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
140
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
146
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
149
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
153
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
156
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
159
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
187
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
194
3643
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
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
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
199
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
202
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
214
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 class LoadFileDialog(FileDialog):
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
221
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 """File selection dialog which checks that the file exists."""
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
223
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 title = "Load File Selection Dialog"
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
225
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 class SaveFileDialog(FileDialog):
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
235
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 """File selection dialog which checks that the file may be created."""
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
237
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 title = "Save File Selection Dialog"
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
239
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 def test():
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 """Simple test program."""
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 root = Tk()
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 root.withdraw()
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 fd = LoadFileDialog(root)
3643
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
267 loadfile = fd.go(key="test")
3031
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 fd = SaveFileDialog(root)
3643
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
269 savefile = fd.go(key="test")
3031
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 print loadfile, savefile
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 if __name__ == '__main__':
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 test()