Terry Jan Reedy | 8675799 | 2014-10-09 18:44:32 -0400 | [diff] [blame] | 1 | # tk common color chooser dialogue |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 2 | # |
| 3 | # this module provides an interface to the native color dialogue |
| 4 | # available in Tk 4.2 and newer. |
| 5 | # |
| 6 | # written by Fredrik Lundh, May 1997 |
| 7 | # |
Guido van Rossum | 5ff1761 | 1998-08-07 14:55:21 +0000 | [diff] [blame] | 8 | # fixed initialcolor handling in August 1998 |
| 9 | # |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 10 | |
| 11 | # |
| 12 | # options (all have default values): |
| 13 | # |
Terry Jan Reedy | 8675799 | 2014-10-09 18:44:32 -0400 | [diff] [blame] | 14 | # - initialcolor: color to mark as selected when dialog is displayed |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 15 | # (given as an RGB triplet or a Tk color string) |
| 16 | # |
| 17 | # - parent: which window to place the dialog on top of |
| 18 | # |
| 19 | # - title: dialog title |
| 20 | # |
| 21 | |
Georg Brandl | 14fc427 | 2008-05-17 18:39:55 +0000 | [diff] [blame] | 22 | from tkinter.commondialog import Dialog |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 23 | |
| 24 | |
| 25 | # |
| 26 | # color chooser class |
| 27 | |
| 28 | class Chooser(Dialog): |
| 29 | "Ask for a color" |
| 30 | |
| 31 | command = "tk_chooseColor" |
| 32 | |
| 33 | def _fixoptions(self): |
| 34 | try: |
| 35 | # make sure initialcolor is a tk color string |
| 36 | color = self.options["initialcolor"] |
Martin v. Löwis | 1f1620e | 2008-12-29 16:27:13 +0000 | [diff] [blame] | 37 | if isinstance(color, tuple): |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 38 | # assume an RGB triplet |
Guido van Rossum | 5ff1761 | 1998-08-07 14:55:21 +0000 | [diff] [blame] | 39 | self.options["initialcolor"] = "#%02x%02x%02x" % color |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 40 | except KeyError: |
| 41 | pass |
| 42 | |
| 43 | def _fixresult(self, widget, result): |
Martin v. Löwis | 1f1620e | 2008-12-29 16:27:13 +0000 | [diff] [blame] | 44 | # result can be somethings: an empty tuple, an empty string or |
| 45 | # a Tcl_Obj, so this somewhat weird check handles that |
| 46 | if not result or not str(result): |
| 47 | return None, None # canceled |
| 48 | |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 49 | # to simplify application code, the color chooser returns |
| 50 | # an RGB tuple together with the Tk color string |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 51 | r, g, b = widget.winfo_rgb(result) |
Martin v. Löwis | 1f1620e | 2008-12-29 16:27:13 +0000 | [diff] [blame] | 52 | return (r/256, g/256, b/256), str(result) |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 53 | |
| 54 | |
| 55 | # |
| 56 | # convenience stuff |
| 57 | |
| 58 | def askcolor(color = None, **options): |
| 59 | "Ask for a color" |
| 60 | |
Guido van Rossum | 5ff1761 | 1998-08-07 14:55:21 +0000 | [diff] [blame] | 61 | if color: |
Guido van Rossum | 2b427c7 | 1998-08-10 20:13:17 +0000 | [diff] [blame] | 62 | options = options.copy() |
| 63 | options["initialcolor"] = color |
Guido van Rossum | 5ff1761 | 1998-08-07 14:55:21 +0000 | [diff] [blame] | 64 | |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 65 | return Chooser(**options).show() |
Guido van Rossum | 1e8c8a2 | 1997-07-19 20:02:36 +0000 | [diff] [blame] | 66 | |
| 67 | |
| 68 | # -------------------------------------------------------------------- |
| 69 | # test stuff |
| 70 | |
| 71 | if __name__ == "__main__": |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 72 | print("color", askcolor()) |