annotate Lib/lib-tk/FileDialog.py @ 58765:2ba7677bfa95 legacy-trunk

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