1 """Main Pynche (Pythonically Natural Color and Hue Editor) widget.
3 This window provides the basic decorations, primarily including the menubar.
4 It is used to bring up other windows.
14 # Milliseconds between interrupt checks
20 def __init__(self
, version
, switchboard
, master
=None, extrapath
=[]):
21 self
.__sb
= switchboard
22 self
.__version
= version
25 self
.__detailswin
= None
27 self
.__dialogstate
= {}
28 modal
= self
.__modal
= not not master
29 # If a master was given, we are running as a modal dialog servant to
30 # some other application. We rearrange our UI in this case (there's
31 # no File menu and we get `Okay' and `Cancel' buttons), and we do a
32 # grab_set() to make ourselves modal
34 self
.__tkroot
= tkroot
= Toplevel(master
, class_
='Pynche')
38 # Is there already a default root for Tk, say because we're
39 # running under Guido's IDE? :-) Two conditions say no, either the
40 # import fails or _default_root is None.
43 from Tkinter
import _default_root
44 tkroot
= self
.__tkroot
= _default_root
48 tkroot
= self
.__tkroot
= Tk(className
='Pynche')
49 # but this isn't our top level widget, so make it invisible
52 menubar
= self
.__menubar
= Menu(tkroot
)
56 filemenu
= self
.__filemenu
= Menu(menubar
, tearoff
=0)
57 filemenu
.add_command(label
='Load palette...',
61 filemenu
.add_command(label
='Quit',
68 views
= make_view_popups(self
.__sb
, self
.__tkroot
, extrapath
)
69 viewmenu
= Menu(menubar
, tearoff
=0)
71 viewmenu
.add_command(label
=v
.menutext(),
73 underline
=v
.underline())
77 helpmenu
= Menu(menubar
, name
='help', tearoff
=0)
78 helpmenu
.add_command(label
='About Pynche...',
79 command
=self
.__popup
_about
,
81 helpmenu
.add_command(label
='Help...',
82 command
=self
.__popup
_usage
,
85 # Tie them all together
87 menubar
.add_cascade(label
='File',
90 menubar
.add_cascade(label
='View',
93 menubar
.add_cascade(label
='Help',
97 # now create the top level window
98 root
= self
.__root
= Toplevel(tkroot
, class_
='Pynche', menu
=menubar
)
99 root
.protocol('WM_DELETE_WINDOW',
100 modal
and self
.__bell
or self
.__quit
)
101 root
.title('Pynche %s' % version
)
102 root
.iconname('Pynche')
103 # Only bind accelerators for the File->Quit menu item if running as a
106 root
.bind('<Alt-q>', self
.__quit
)
107 root
.bind('<Alt-Q>', self
.__quit
)
109 # We're a modal dialog so we have a new row of buttons
110 bframe
= Frame(root
, borderwidth
=1, relief
=RAISED
)
111 bframe
.grid(row
=4, column
=0, columnspan
=2,
114 okay
= Button(bframe
,
117 okay
.pack(side
=LEFT
, expand
=1)
118 cancel
= Button(bframe
,
120 command
=self
.__cancel
)
121 cancel
.pack(side
=LEFT
, expand
=1)
123 def __quit(self
, event
=None):
126 def __bell(self
, event
=None):
129 def __okay(self
, event
=None):
130 self
.__sb
.withdraw_views()
131 self
.__tkroot
.grab_release()
134 def __cancel(self
, event
=None):
138 def __keepalive(self
):
139 # Exercise the Python interpreter regularly so keyboard interrupts get
141 self
.__tkroot
.tk
.createtimerhandler(KEEPALIVE_TIMER
, self
.__keepalive
)
146 self
.__tkroot
.mainloop()
151 def __popup_about(self
, event
=None):
152 from Main
import __version__
153 tkMessageBox
.showinfo('About Pynche ' + __version__
,
156 The PYthonically Natural
160 contact: Barry A. Warsaw
161 email: bwarsaw@python.org''' % __version__
)
163 def __popup_usage(self
, event
=None):
164 if not self
.__helpwin
:
165 self
.__helpwin
= Helpwin(self
.__root
, self
.__quit
)
166 self
.__helpwin
.deiconify()
168 def __load(self
, event
=None):
170 idir
, ifile
= os
.path
.split(self
.__sb
.colordb().filename())
171 file = tkFileDialog
.askopenfilename(
172 filetypes
=[('Text files', '*.txt'),
181 colordb
= ColorDB
.get_colordb(file)
183 tkMessageBox
.showerror('Read error', '''\
184 Could not open file for reading:
188 tkMessageBox
.showerror('Unrecognized color file type', '''\
189 Unrecognized color file type in file:
193 self
.__sb
.set_colordb(colordb
)
196 self
.__root
.withdraw()
199 self
.__root
.deiconify()
204 def __init__(self
, master
, quitfunc
):
205 from Main
import docstring
206 self
.__root
= root
= Toplevel(master
, class_
='Pynche')
207 root
.protocol('WM_DELETE_WINDOW', self
.__withdraw
)
208 root
.title('Pynche Help Window')
209 root
.iconname('Pynche Help Window')
210 root
.bind('<Alt-q>', quitfunc
)
211 root
.bind('<Alt-Q>', quitfunc
)
212 root
.bind('<Alt-w>', self
.__withdraw
)
213 root
.bind('<Alt-W>', self
.__withdraw
)
215 # more elaborate help is available in the README file
216 readmefile
= os
.path
.join(sys
.path
[0], 'README')
220 fp
= open(readmefile
)
222 # wax the last page, it contains Emacs cruft
223 i
= contents
.rfind('\f')
225 contents
= contents
[:i
].rstrip()
230 sys
.stderr
.write("Couldn't open Pynche's README, "
231 'using docstring instead.\n')
232 contents
= docstring()
234 self
.__text
= text
= Text(root
, relief
=SUNKEN
,
236 self
.__text
.focus_set()
237 text
.insert(0.0, contents
)
238 scrollbar
= Scrollbar(root
)
239 scrollbar
.pack(fill
=Y
, side
=RIGHT
)
240 text
.pack(fill
=BOTH
, expand
=YES
)
241 text
.configure(yscrollcommand
=(scrollbar
, 'set'))
242 scrollbar
.configure(command
=(text
, 'yview'))
244 def __withdraw(self
, event
=None):
245 self
.__root
.withdraw()
248 self
.__root
.deiconify()
253 def __init__(self
, module
, name
, switchboard
, root
):
256 self
.__sb
= switchboard
258 self
.__menutext
= module
.ADDTOVIEW
259 # find the underline character
260 underline
= module
.ADDTOVIEW
.find('%')
264 self
.__menutext
= module
.ADDTOVIEW
.replace('%', '', 1)
265 self
.__underline
= underline
269 return self
.__menutext
272 return self
.__underline
274 def popup(self
, event
=None):
275 if not self
.__window
:
276 # class and module must have the same name
277 class_
= getattr(self
.__m
, self
.__name
)
278 self
.__window
= class_(self
.__sb
, self
.__root
)
279 self
.__sb
.add_view(self
.__window
)
280 self
.__window
.deiconify()
282 def __cmp__(self
, other
):
283 return cmp(self
.__menutext
, other
.__menutext
)
286 def make_view_popups(switchboard
, root
, extrapath
):
288 # where we are in the file system
289 dirs
= [os
.path
.dirname(__file__
)] + extrapath
293 for file in os
.listdir(dir):
294 if file[-9:] == 'Viewer.py':
297 module
= __import__(name
)
299 # Pynche is running from inside a package, so get the
300 # module using the explicit path.
301 pkg
= __import__('pynche.'+name
)
302 module
= getattr(pkg
, name
)
303 if hasattr(module
, 'ADDTOVIEW') and module
.ADDTOVIEW
:
304 # this is an external viewer
305 v
= PopupViewer(module
, name
, switchboard
, root
)
307 # sort alphabetically