Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | # |
2 | # Instant Python | |
3 | # $Id: tkColorChooser.py,v 1.6 2003/04/06 09:00:52 rhettinger 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 | # fixed initialcolor handling in August 1998 | |
13 | # | |
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 | ||
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 | |
43 | self.options["initialcolor"] = "#%02x%02x%02x" % color | |
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: | |
51 | return None, None # canceled | |
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 | ||
62 | if color: | |
63 | options = options.copy() | |
64 | options["initialcolor"] = color | |
65 | ||
66 | return Chooser(**options).show() | |
67 | ||
68 | ||
69 | # -------------------------------------------------------------------- | |
70 | # test stuff | |
71 | ||
72 | if __name__ == "__main__": | |
73 | ||
74 | print "color", askcolor() |