Many changes to support a second mode of operation. Pynche can now be

run either as a standalone application (by running pynche or
pynche.pyw), or as a modal dialog inside another application.  This
can be done by importing pyColorChooser and running askcolor().  The
API for this is the same as the tkColorChooser.askcolor() API, namely:

    When `Okay' is hit, askcolor() returns ((r, g, b), "name").  When
    `Cancel' is hit, askcolor() returns (None, None).

Note the following differences:

    1. pyColorChooser.askcolor() takes an optional keyword `master'
       which if set tells Pynche to run as a modal dialog.  `master'
       is a Tkinter parent window.  Without the `master' keyword
       Pynche runs standalone.

    2. in pyColorChooser.askcolor() will return a Tk/X11 color name as
       "name" if there is an exact match, otherwise it will return a
       color spec, e.g. "#rrggbb".  tkColorChooser can't return a
       color name.

There are also some UI differences when running standalone vs. modal.
When modal, there is no "File" menu, but instead there are "Okay" and
"Cancel" buttons.

The implementation of all this is a bit of a hack, but it seems to
work moderately well.  I'm not guaranteeing the pyColorChooser.Chooser
class has the same semantics as the tkColorChooser.Chooser class.
This commit is contained in:
Barry Warsaw 1998-10-22 03:25:59 +00:00
parent 04da10c7a2
commit ca07ba00ac
10 changed files with 266 additions and 130 deletions

View file

@ -13,36 +13,46 @@ KEEPALIVE_TIMER = 500
class PyncheWidget:
def __init__(self, version, switchboard):
def __init__(self, version, switchboard, master=None):
self.__sb = switchboard
self.__version = version
self.__textwin = None
self.__listwin = None
self.__detailswin = None
#
# Is there already a default root for Tk, say because we're running
# under Guido's IDE? :-) Two conditions say no, either the import
# fails or _default_root is None.
tkroot = None
try:
from Tkinter import _default_root
tkroot = self.__tkroot = _default_root
except ImportError:
pass
if not tkroot:
tkroot = self.__tkroot = Tk(className='Pynche')
# but this isn't our top level widget, so make it invisible
tkroot.withdraw()
modal = self.__modal = not not master
# If a master was given, we are running as a modal dialog servant to
# some other application. We rearrange our UI in this case (there's
# no File menu and we get `Okay' and `Cancel' buttons), and we do a
# grab_set() to make ourselves modal
if modal:
self.__tkroot = tkroot = Toplevel(master, class_='Pynche')
tkroot.grab_set()
tkroot.withdraw()
else:
# Is there already a default root for Tk, say because we're
# running under Guido's IDE? :-) Two conditions say no, either the
# import fails or _default_root is None.
tkroot = None
try:
from Tkinter import _default_root
tkroot = self.__tkroot = _default_root
except ImportError:
pass
if not tkroot:
tkroot = self.__tkroot = Tk(className='Pynche')
# but this isn't our top level widget, so make it invisible
tkroot.withdraw()
# create the menubar
menubar = self.__menubar = Menu(tkroot)
#
# File menu
#
filemenu = self.__filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label='Quit',
command=self.__quit,
accelerator='Alt-Q',
underline=0)
if not modal:
filemenu = self.__filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label='Quit',
command=self.__quit,
accelerator='Alt-Q',
underline=0)
#
# View menu
#
@ -66,9 +76,10 @@ class PyncheWidget:
#
# Tie them all together
#
menubar.add_cascade(label='File',
menu=filemenu,
underline=0)
if not modal:
menubar.add_cascade(label='File',
menu=filemenu,
underline=0)
menubar.add_cascade(label='View',
menu=viewmenu,
underline=0)
@ -78,15 +89,44 @@ class PyncheWidget:
# now create the top level window
root = self.__root = Toplevel(tkroot, class_='Pynche', menu=menubar)
root.protocol('WM_DELETE_WINDOW', self.__quit)
root.protocol('WM_DELETE_WINDOW',
modal and self.__beep or self.__quit)
root.title('Pynche %s' % version)
root.iconname('Pynche')
root.tk.createtimerhandler(KEEPALIVE_TIMER, self.__keepalive)
root.bind('<Alt-q>', self.__quit)
root.bind('<Alt-Q>', self.__quit)
# Only bind accelerators for the File->Quit menu item if running as a
# standalone app
if not modal:
root.bind('<Alt-q>', self.__quit)
root.bind('<Alt-Q>', self.__quit)
else:
# We're a modal dialog so we have a new row of buttons
bframe = Frame(root, borderwidth=1, relief=RAISED)
bframe.grid(row=4, column=0, columnspan=2,
sticky='EW',
ipady=5)
okay = Button(bframe,
text='Okay',
command=self.__okay)
okay.pack(side=LEFT, expand=1)
cancel = Button(bframe,
text='Cancel',
command=self.__cancel)
cancel.pack(side=LEFT, expand=1)
def __quit(self, event=None):
self.__root.quit()
self.__tkroot.quit()
def __beep(self, event=None):
self.__tkroot.beep()
def __okay(self, event=None):
self.__sb.withdraw_views()
self.__tkroot.grab_release()
self.__quit()
def __cancel(self, event=None):
self.__sb.canceled()
self.__okay()
def __keepalive(self):
# Exercise the Python interpreter regularly so keyboard interrupts get
@ -94,10 +134,11 @@ class PyncheWidget:
self.__tkroot.tk.createtimerhandler(KEEPALIVE_TIMER, self.__keepalive)
def start(self):
self.__keepalive()
if not self.__modal:
self.__keepalive()
self.__tkroot.mainloop()
def parent(self):
def window(self):
return self.__root
def __popup_about(self, event=None):
@ -135,3 +176,6 @@ email : bwarsaw@python.org''' % __version__)
self.__detailswin = DetailsViewer(self.__sb, self.__root)
self.__sb.add_view(self.__detailswin)
self.__detailswin.deiconify()
def withdraw(self):
self.__root.withdraw()