blob: e3593ed24c835464fc467a47e1e37f8a15073da6 [file] [log] [blame]
Guido van Rossum1e8c8a21997-07-19 20:02:36 +00001# tk common colour chooser dialogue
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 Rossum5ff17611998-08-07 14:55:21 +00008# fixed initialcolor handling in August 1998
9#
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000010
11#
12# options (all have default values):
13#
14# - initialcolor: colour to mark as selected when dialog is displayed
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
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000022from tkCommonDialog import Dialog
23
24
25#
26# color chooser class
27
28class 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"]
37 if type(color) == type(()):
38 # assume an RGB triplet
Guido van Rossum5ff17611998-08-07 14:55:21 +000039 self.options["initialcolor"] = "#%02x%02x%02x" % color
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000040 except KeyError:
41 pass
42
43 def _fixresult(self, widget, result):
44 # to simplify application code, the color chooser returns
45 # an RGB tuple together with the Tk color string
46 if not result:
Thomas Wouters7e474022000-07-16 12:04:32 +000047 return None, None # canceled
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000048 r, g, b = widget.winfo_rgb(result)
49 return (r/256, g/256, b/256), result
50
51
52#
53# convenience stuff
54
55def askcolor(color = None, **options):
56 "Ask for a color"
57
Guido van Rossum5ff17611998-08-07 14:55:21 +000058 if color:
Guido van Rossum2b427c71998-08-10 20:13:17 +000059 options = options.copy()
60 options["initialcolor"] = color
Guido van Rossum5ff17611998-08-07 14:55:21 +000061
Raymond Hettingerff41c482003-04-06 09:01:11 +000062 return Chooser(**options).show()
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000063
64
65# --------------------------------------------------------------------
66# test stuff
67
68if __name__ == "__main__":
69
Guido van Rossumbe19ed72007-02-09 05:37:30 +000070 print("color", askcolor())