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 | # |
| 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 |
Guido van Rossum | 1530c87 | 1997-08-14 14:17:28 +0000 | [diff] [blame] | 25 | # doesn't seem to work properly on true colour displays. maybe we |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 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() |
Guido van Rossum | 1530c87 | 1997-08-14 14:17:28 +0000 | [diff] [blame] | 73 | |