annotate Lib/tkinter/filedialog.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents 7239fbcdcd4f
children fa82071bb7e1
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
47132
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
9 This module also presents tk common file dialogues, it provides interfaces
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
10 to the native file dialogues available in Tk 4.2 and newer, and the
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
11 directory dialogue available in Tk 8.3 and newer.
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
12 These interfaces were written by Fredrik Lundh, May 1997.
3031
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
47130
ef4cca2d6171 Tkinter, step 2: adapt imports.
Georg Brandl <georg@python.org>
parents: 47129
diff changeset
15 from tkinter import *
ef4cca2d6171 Tkinter, step 2: adapt imports.
Georg Brandl <georg@python.org>
parents: 47129
diff changeset
16 from tkinter.dialog import Dialog
47132
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
17 from tkinter import commondialog
3031
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 import os
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 import fnmatch
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
3643
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
23 dialogstates = {}
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
24
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
25
3031
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 class FileDialog:
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 """Standard file selection dialog -- no checks on selected file.
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 Usage:
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 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
33 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
34 if fname is None: ...canceled...
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
35 else: ...open file...
3643
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
36
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
37 All arguments to go() are optional.
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
38
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
39 The 'key' argument specifies a key in the global dictionary
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
40 'dialogstates', which keeps track of the values for the directory
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
41 and pattern arguments, overriding the values passed in (it does
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
42 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
43 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
44 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
45 behavior of the Macintosh file selection dialogs.)
3031
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
46
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 """
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 title = "File Selection Dialog"
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
3643
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
51 def __init__(self, master, title=None):
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
52 if title is None: title = self.title
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
53 self.master = master
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
54 self.directory = None
3643
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
55
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
56 self.top = Toplevel(master)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
57 self.top.title(title)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
58 self.top.iconname(title)
3643
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
59
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
60 self.botframe = Frame(self.top)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
61 self.botframe.pack(side=BOTTOM, fill=X)
3643
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
62
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
63 self.selection = Entry(self.top)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
64 self.selection.pack(side=BOTTOM, fill=X)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
65 self.selection.bind('<Return>', self.ok_event)
3643
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
66
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
67 self.filter = Entry(self.top)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
68 self.filter.pack(side=TOP, fill=X)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
69 self.filter.bind('<Return>', self.filter_command)
3643
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
70
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
71 self.midframe = Frame(self.top)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
72 self.midframe.pack(expand=YES, fill=BOTH)
3643
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
73
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
74 self.filesbar = Scrollbar(self.midframe)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
75 self.filesbar.pack(side=RIGHT, fill=Y)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
76 self.files = Listbox(self.midframe, exportselection=0,
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
77 yscrollcommand=(self.filesbar, 'set'))
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
78 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
79 btags = self.files.bindtags()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
80 self.files.bindtags(btags[1:] + btags[:1])
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
81 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
82 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
83 self.filesbar.config(command=(self.files, 'yview'))
3643
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
84
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
85 self.dirsbar = Scrollbar(self.midframe)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
86 self.dirsbar.pack(side=LEFT, fill=Y)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
87 self.dirs = Listbox(self.midframe, exportselection=0,
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
88 yscrollcommand=(self.dirsbar, 'set'))
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
89 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
90 self.dirsbar.config(command=(self.dirs, 'yview'))
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
91 btags = self.dirs.bindtags()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
92 self.dirs.bindtags(btags[1:] + btags[:1])
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
93 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
94 self.dirs.bind('<Double-ButtonRelease-1>', self.dirs_double_event)
3643
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
95
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
96 self.ok_button = Button(self.botframe,
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
97 text="OK",
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
98 command=self.ok_command)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
99 self.ok_button.pack(side=LEFT)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
100 self.filter_button = Button(self.botframe,
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
101 text="Filter",
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
102 command=self.filter_command)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
103 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
104 self.cancel_button = Button(self.botframe,
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
105 text="Cancel",
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
106 command=self.cancel_command)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
107 self.cancel_button.pack(side=RIGHT)
3031
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
108
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
109 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
110 # 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
111 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
112 self.top.bind('<Alt-W>', self.cancel_command)
3643
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
113
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
114 def go(self, dir_or_file=os.curdir, pattern="*", default="", key=None):
39592
0f135affabf2 Couple more has_keys() going...
Guido van Rossum <guido@python.org>
parents: 32940
diff changeset
115 if key and key in dialogstates:
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
116 self.directory, pattern = dialogstates[key]
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
117 else:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
118 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
119 if os.path.isdir(dir_or_file):
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
120 self.directory = dir_or_file
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
121 else:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
122 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
123 self.set_filter(self.directory, pattern)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
124 self.set_selection(default)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
125 self.filter_command()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
126 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
127 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
128 self.top.grab_set()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
129 self.how = None
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
130 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
131 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
132 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
133 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
134 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
135 dialogstates[key] = directory, pattern
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
136 self.top.destroy()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
137 return self.how
3643
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
138
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
139 def quit(self, how=None):
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
140 self.how = how
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
141 self.master.quit() # Exit mainloop()
3031
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
142
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 def dirs_double_event(self, event):
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
144 self.filter_command()
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 dirs_select_event(self, event):
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
147 dir, pat = self.get_filter()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
148 subdir = self.dirs.get('active')
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
149 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
150 self.set_filter(dir, pat)
3031
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
151
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 def files_double_event(self, event):
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
153 self.ok_command()
3031
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
154
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 def files_select_event(self, event):
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
156 file = self.files.get('active')
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
157 self.set_selection(file)
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 ok_event(self, event):
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
160 self.ok_command()
3031
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
161
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 def ok_command(self):
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
163 self.quit(self.get_selection())
3031
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
164
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 def filter_command(self, event=None):
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
166 dir, pat = self.get_filter()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
167 try:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
168 names = os.listdir(dir)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
169 except os.error:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
170 self.master.bell()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
171 return
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
172 self.directory = dir
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
173 self.set_filter(dir, pat)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
174 names.sort()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
175 subdirs = [os.pardir]
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
176 matchingfiles = []
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
177 for name in names:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
178 fullname = os.path.join(dir, name)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
179 if os.path.isdir(fullname):
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
180 subdirs.append(name)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
181 elif fnmatch.fnmatch(name, pat):
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
182 matchingfiles.append(name)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
183 self.dirs.delete(0, END)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
184 for name in subdirs:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
185 self.dirs.insert(END, name)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
186 self.files.delete(0, END)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
187 for name in matchingfiles:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
188 self.files.insert(END, name)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
189 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
190 if tail == os.curdir: tail = ''
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
191 self.set_selection(tail)
3031
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
192
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 def get_filter(self):
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
194 filter = self.filter.get()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
195 filter = os.path.expanduser(filter)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
196 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
197 filter = os.path.join(filter, "*")
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
198 return os.path.split(filter)
3031
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
199
3643
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
200 def get_selection(self):
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
201 file = self.selection.get()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
202 file = os.path.expanduser(file)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
203 return file
3643
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
204
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
205 def cancel_command(self, event=None):
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
206 self.quit()
3031
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
207
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 def set_filter(self, dir, pat):
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
209 if not os.path.isabs(dir):
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
210 try:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
211 pwd = os.getcwd()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
212 except os.error:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
213 pwd = None
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
214 if pwd:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
215 dir = os.path.join(pwd, dir)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
216 dir = os.path.normpath(dir)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
217 self.filter.delete(0, END)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
218 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
219
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 def set_selection(self, file):
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
221 self.selection.delete(0, END)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
222 self.selection.insert(END, os.path.join(self.directory, file))
3031
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 class LoadFileDialog(FileDialog):
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
226
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 """File selection dialog which checks that the file exists."""
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
228
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 title = "Load File Selection Dialog"
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
230
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 def ok_command(self):
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
232 file = self.get_selection()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
233 if not os.path.isfile(file):
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
234 self.master.bell()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
235 else:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
236 self.quit(file)
3031
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
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 class SaveFileDialog(FileDialog):
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
240
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 """File selection dialog which checks that the file may be created."""
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
242
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 title = "Save File Selection Dialog"
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
244
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 def ok_command(self):
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
246 file = self.get_selection()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
247 if os.path.exists(file):
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
248 if os.path.isdir(file):
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
249 self.master.bell()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
250 return
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
251 d = Dialog(self.top,
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
252 title="Overwrite Existing File Question",
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27325
diff changeset
253 text="Overwrite existing file %r?" % (file,),
7731
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
254 bitmap='questhead',
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
255 default=1,
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
256 strings=("Yes", "Cancel"))
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
257 if d.num != 0:
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 else:
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
260 head, tail = os.path.split(file)
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
261 if not os.path.isdir(head):
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
262 self.master.bell()
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
263 return
9c37ed8868b9 Give in to the tab police.
Guido van Rossum <guido@python.org>
parents: 4893
diff changeset
264 self.quit(file)
3031
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
265
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
266
47132
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
267
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
268 # For the following classes and modules:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
269 #
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
270 # options (all have default values):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
271 #
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
272 # - defaultextension: added to filename if not explicitly given
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
273 #
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
274 # - filetypes: sequence of (label, pattern) tuples. the same pattern
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
275 # may occur with several patterns. use "*" as pattern to indicate
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
276 # all files.
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
277 #
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
278 # - initialdir: initial directory. preserved by dialog instance.
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
279 #
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
280 # - initialfile: initial file (ignored by the open dialog). preserved
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
281 # by dialog instance.
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
282 #
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
283 # - parent: which window to place the dialog on top of
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
284 #
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
285 # - title: dialog title
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
286 #
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
287 # - multiple: if true user may select more than one file
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
288 #
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
289 # options for the directory chooser:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
290 #
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
291 # - initialdir, parent, title: see above
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
292 #
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
293 # - mustexist: if true, user must pick an existing directory
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
294 #
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
295
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
296
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
297 class _Dialog(commondialog.Dialog):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
298
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
299 def _fixoptions(self):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
300 try:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
301 # make sure "filetypes" is a tuple
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
302 self.options["filetypes"] = tuple(self.options["filetypes"])
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
303 except KeyError:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
304 pass
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
305
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
306 def _fixresult(self, widget, result):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
307 if result:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
308 # keep directory and filename until next time
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
309 import os
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
310 # convert Tcl path objects to strings
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
311 try:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
312 result = result.string
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
313 except AttributeError:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
314 # it already is a string
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
315 pass
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
316 path, file = os.path.split(result)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
317 self.options["initialdir"] = path
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
318 self.options["initialfile"] = file
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
319 self.filename = result # compatibility
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
320 return result
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
321
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
322
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
323 #
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
324 # file dialogs
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
325
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
326 class Open(_Dialog):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
327 "Ask for a filename to open"
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
328
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
329 command = "tk_getOpenFile"
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
330
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
331 def _fixresult(self, widget, result):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
332 if isinstance(result, tuple):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
333 # multiple results:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
334 result = tuple([getattr(r, "string", r) for r in result])
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
335 if result:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
336 import os
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
337 path, file = os.path.split(result[0])
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
338 self.options["initialdir"] = path
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
339 # don't set initialfile or filename, as we have multiple of these
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
340 return result
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
341 if not widget.tk.wantobjects() and "multiple" in self.options:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
342 # Need to split result explicitly
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
343 return self._fixresult(widget, widget.tk.splitlist(result))
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
344 return _Dialog._fixresult(self, widget, result)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
345
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
346 class SaveAs(_Dialog):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
347 "Ask for a filename to save as"
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
348
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
349 command = "tk_getSaveFile"
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
350
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
351
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
352 # the directory dialog has its own _fix routines.
50401
7239fbcdcd4f Merged revisions 68103 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47132
diff changeset
353 class Directory(commondialog.Dialog):
47132
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
354 "Ask for a directory"
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
355
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
356 command = "tk_chooseDirectory"
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
357
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
358 def _fixresult(self, widget, result):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
359 if result:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
360 # convert Tcl path objects to strings
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
361 try:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
362 result = result.string
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
363 except AttributeError:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
364 # it already is a string
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
365 pass
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
366 # keep directory until next time
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
367 self.options["initialdir"] = result
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
368 self.directory = result # compatibility
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
369 return result
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
370
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
371 #
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
372 # convenience stuff
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
373
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
374 def askopenfilename(**options):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
375 "Ask for a filename to open"
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
376
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
377 return Open(**options).show()
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
378
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
379 def asksaveasfilename(**options):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
380 "Ask for a filename to save as"
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
381
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
382 return SaveAs(**options).show()
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
383
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
384 def askopenfilenames(**options):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
385 """Ask for multiple filenames to open
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
386
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
387 Returns a list of filenames or empty list if
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
388 cancel button selected
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
389 """
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
390 options["multiple"]=1
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
391 return Open(**options).show()
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
392
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
393 # FIXME: are the following perhaps a bit too convenient?
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
394
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
395 def askopenfile(mode = "r", **options):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
396 "Ask for a filename to open, and returned the opened file"
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
397
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
398 filename = Open(**options).show()
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
399 if filename:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
400 return open(filename, mode)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
401 return None
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
402
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
403 def askopenfiles(mode = "r", **options):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
404 """Ask for multiple filenames and return the open file
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
405 objects
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
406
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
407 returns a list of open file objects or an empty list if
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
408 cancel selected
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
409 """
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
410
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
411 files = askopenfilenames(**options)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
412 if files:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
413 ofiles=[]
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
414 for filename in files:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
415 ofiles.append(open(filename, mode))
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
416 files=ofiles
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
417 return files
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
418
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
419
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
420 def asksaveasfile(mode = "w", **options):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
421 "Ask for a filename to save as, and returned the opened file"
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
422
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
423 filename = SaveAs(**options).show()
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
424 if filename:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
425 return open(filename, mode)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
426 return None
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
427
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
428 def askdirectory (**options):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
429 "Ask for a directory, and return the file name"
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
430 return Directory(**options).show()
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
431
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
432
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
433
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
434 # --------------------------------------------------------------------
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
435 # test stuff
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
436
3031
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
437 def test():
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
438 """Simple test program."""
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
439 root = Tk()
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
440 root.withdraw()
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
441 fd = LoadFileDialog(root)
3643
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
442 loadfile = fd.go(key="test")
3031
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
443 fd = SaveFileDialog(root)
3643
0d1463a45b6f Major overhaul:
Guido van Rossum <guido@python.org>
parents: 3642
diff changeset
444 savefile = fd.go(key="test")
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 39592
diff changeset
445 print(loadfile, savefile)
3031
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
446
47132
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
447 # Since the file name may contain non-ASCII characters, we need
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
448 # to find an encoding that likely supports the file name, and
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
449 # displays correctly on the terminal.
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
450
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
451 # Start off with UTF-8
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
452 enc = "utf-8"
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
453 import sys
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
454
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
455 # See whether CODESET is defined
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
456 try:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
457 import locale
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
458 locale.setlocale(locale.LC_ALL,'')
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
459 enc = locale.nl_langinfo(locale.CODESET)
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
460 except (ImportError, AttributeError):
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
461 pass
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
462
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
463 # dialog for openening files
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
464
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
465 openfilename=askopenfilename(filetypes=[("all files", "*")])
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
466 try:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
467 fp=open(openfilename,"r")
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
468 fp.close()
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
469 except:
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
470 print("Could not open File: ")
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
471 print(sys.exc_info()[1])
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
472
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
473 print("open", openfilename.encode(enc))
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
474
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
475 # dialog for saving files
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
476
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
477 saveasfilename=asksaveasfilename()
8a74612f3d2b Tkinter, step 4: merge simple- and filedialog modules.
Georg Brandl <georg@python.org>
parents: 47130
diff changeset
478 print("saveas", saveasfilename.encode(enc))
3031
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
479
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
480 if __name__ == '__main__':
9c49ace6b1e7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
481 test()