Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 1 | # |
| 2 | # Instant Python |
| 3 | # $Id$ |
| 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 | # |
Guido van Rossum | 5ff1761 | 1998-08-07 14:55:21 +0000 | [diff] [blame] | 12 | # fixed initialcolor handling in August 1998 |
| 13 | # |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 14 | |
| 15 | # |
| 16 | # options (all have default values): |
| 17 | # |
| 18 | # - initialcolor: colour to mark as selected when dialog is displayed |
| 19 | # (given as an RGB triplet or a Tk color string) |
| 20 | # |
| 21 | # - parent: which window to place the dialog on top of |
| 22 | # |
| 23 | # - title: dialog title |
| 24 | # |
| 25 | |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 26 | from tkCommonDialog import Dialog |
| 27 | |
| 28 | |
| 29 | # |
| 30 | # color chooser class |
| 31 | |
| 32 | class Chooser(Dialog): |
| 33 | "Ask for a color" |
| 34 | |
| 35 | command = "tk_chooseColor" |
| 36 | |
| 37 | def _fixoptions(self): |
| 38 | try: |
| 39 | # make sure initialcolor is a tk color string |
| 40 | color = self.options["initialcolor"] |
| 41 | if type(color) == type(()): |
| 42 | # assume an RGB triplet |
Guido van Rossum | 5ff1761 | 1998-08-07 14:55:21 +0000 | [diff] [blame] | 43 | self.options["initialcolor"] = "#%02x%02x%02x" % color |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 44 | except KeyError: |
| 45 | pass |
| 46 | |
| 47 | def _fixresult(self, widget, result): |
| 48 | # to simplify application code, the color chooser returns |
| 49 | # an RGB tuple together with the Tk color string |
| 50 | if not result: |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 51 | return None, None # canceled |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 52 | r, g, b = widget.winfo_rgb(result) |
| 53 | return (r/256, g/256, b/256), result |
| 54 | |
| 55 | |
| 56 | # |
| 57 | # convenience stuff |
| 58 | |
| 59 | def askcolor(color = None, **options): |
| 60 | "Ask for a color" |
| 61 | |
Guido van Rossum | 5ff1761 | 1998-08-07 14:55:21 +0000 | [diff] [blame] | 62 | if color: |
Guido van Rossum | 2b427c7 | 1998-08-10 20:13:17 +0000 | [diff] [blame] | 63 | options = options.copy() |
| 64 | options["initialcolor"] = color |
Guido van Rossum | 5ff1761 | 1998-08-07 14:55:21 +0000 | [diff] [blame] | 65 | |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 66 | return Chooser(**options).show() |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 67 | |
| 68 | |
| 69 | # -------------------------------------------------------------------- |
| 70 | # test stuff |
| 71 | |
| 72 | if __name__ == "__main__": |
| 73 | |
| 74 | print "color", askcolor() |