]> granicus.if.org Git - python/commitdiff
Better initial color choosing
authorBarry Warsaw <barry@python.org>
Tue, 6 Oct 1998 19:50:33 +0000 (19:50 +0000)
committerBarry Warsaw <barry@python.org>
Tue, 6 Oct 1998 19:50:33 +0000 (19:50 +0000)
Tools/pynche/Main.py
Tools/pynche/PyncheWidget.py

index 64cd579cc95cdebe6e14b0a11e7225bd747d3123..4877adc6b037beedd0d40ef951b691adf2a4e1fa 100644 (file)
@@ -61,6 +61,34 @@ def usage(status, msg=''):
     sys.exit(status)
 
 
+\f
+def initial_color(s, colordb):
+    # function called on every color
+    def scan_color(s, colordb=colordb):
+        try:
+            r, g, b = colordb.find_byname(s)
+        except ColorDB.BadColor:
+            try:
+                r, g, b = ColorDB.rrggbb_to_triplet(s)
+            except ColorDB.BadColor:
+                return None, None, None
+        return r, g, b
+    #
+    # First try the passed in color
+    r, g, b = scan_color(s)
+    if r is None:
+        # try the same color with '#' prepended, since some shells require
+        # this to be escaped, which is a pain
+        r, g, b = scan_color('#' + s)
+    if r is None:
+        print 'Bad initial color, using gray50:', s
+        r, g, b = scan_color('gray50')
+    if r is None:
+        usage(1, 'Cannot find an initial color to use')
+        # does not return
+    return r, g, b
+
+
 \f
 def main():
     try:
@@ -95,19 +123,8 @@ def main():
     else:
         usage(1, 'No color database file found, see the -d option.')
 
-    # get triplet for initial color
-    try:
-       red, green, blue = colordb.find_byname(initialcolor)
-    except ColorDB.BadColor:
-       # must be a #rrggbb style color
-       try:
-           red, green, blue = ColorDB.rrggbb_to_triplet(initialcolor)
-       except ColorDB.BadColor:
-            try:
-                red, green, blue = ColorDB.rrggbb_to_triplet('#7f7f7f')
-                print 'Bad initial color, using gray50:', initialcolor
-            except ColorDB.BadColor:
-                usage(1, 'Cannot find an initial color to use')
+    # get the initial color as components
+    red, green, blue = initial_color(initialcolor, colordb)
 
     # create all output widgets
     s = Switchboard(colordb)
index 85008ac5ceda467a1d06702448f7f1519224c7a0..a5560cc607e51bbc3b051577d04f93f5e14382ea 100644 (file)
@@ -40,7 +40,7 @@ class PyncheWidget:
         #
         filemenu = self.__filemenu = Menu(menubar, tearoff=0)
         filemenu.add_command(label='Quit',
-                             command=tkroot.quit,
+                             command=self.__quit,
                              accelerator='Alt-Q',
                              underline=0)
         #
@@ -78,12 +78,15 @@ class PyncheWidget:
 
         # now create the top level window
         root = self.__root = Toplevel(tkroot, class_='Pynche', menu=menubar)
-        root.protocol('WM_DELETE_WINDOW', tkroot.quit)
+        root.protocol('WM_DELETE_WINDOW', self.__quit)
         root.title('Pynche %s' % version)
         root.iconname('Pynche')
         root.tk.createtimerhandler(KEEPALIVE_TIMER, self.__keepalive)
-        root.bind('<Alt-q>', tkroot.quit)
-        root.bind('<Alt-Q>', tkroot.quit)
+        root.bind('<Alt-q>', self.__quit)
+        root.bind('<Alt-Q>', self.__quit)
+
+    def __quit(self, event=None):
+        self.__root.quit()
 
     def __keepalive(self):
         # Exercise the Python interpreter regularly so keyboard interrupts get