home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyos2bin.zip / Lib / lib-tk / tkColorChooser.py < prev    next >
Text File  |  1997-08-14  |  2KB  |  74 lines

  1. #
  2. # Instant Python
  3. # tkColorChooser.py,v 1.2 1997/08/14 14:17:23 guido Exp
  4. #
  5. # tk common colour chooser dialogue
  6. #
  7. # this module provides an interface to the native color dialogue
  8. # available in Tk 4.2 and newer.
  9. #
  10. # written by Fredrik Lundh, May 1997
  11. #
  12.  
  13. #
  14. # options (all have default values):
  15. #
  16. # - initialcolor: colour to mark as selected when dialog is displayed
  17. #   (given as an RGB triplet or a Tk color string)
  18. #
  19. # - parent: which window to place the dialog on top of
  20. #
  21. # - title: dialog title
  22. #
  23.  
  24. # FIXME: as of Tk 8.0a2, the Unix colour picker is really ugly, and
  25. # doesn't seem to work properly on true colour displays.  maybe we
  26. # should use the instant python version instead?
  27.  
  28. from tkCommonDialog import Dialog
  29.  
  30.  
  31. #
  32. # color chooser class
  33.  
  34. class Chooser(Dialog):
  35.     "Ask for a color"
  36.  
  37.     command = "tk_chooseColor"
  38.  
  39.     def _fixoptions(self):
  40.         try:
  41.             # make sure initialcolor is a tk color string
  42.             color = self.options["initialcolor"]
  43.             if type(color) == type(()):
  44.                 # assume an RGB triplet
  45.                 self.options["initialcolor"] = "%02x%02x%02x" % color
  46.         except KeyError:
  47.             pass
  48.  
  49.     def _fixresult(self, widget, result):
  50.         # to simplify application code, the color chooser returns
  51.         # an RGB tuple together with the Tk color string
  52.         if not result:
  53.             return None, None # cancelled
  54.         r, g, b = widget.winfo_rgb(result)
  55.         return (r/256, g/256, b/256), result
  56.  
  57.  
  58. #
  59. # convenience stuff
  60.  
  61. def askcolor(color = None, **options):
  62.     "Ask for a color"
  63.  
  64.     return apply(Chooser, (), options).show()
  65.  
  66.  
  67. # --------------------------------------------------------------------
  68. # test stuff
  69.  
  70. if __name__ == "__main__":
  71.  
  72.     print "color", askcolor()
  73.  
  74.