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