5 # tk common colour chooser dialogue
7 # this module provides an interface to the native color dialogue
8 # available in Tk 4.2 and newer.
10 # written by Fredrik Lundh, May 1997
12 # fixed initialcolor handling in August 1998
16 # options (all have default values):
18 # - initialcolor: colour to mark as selected when dialog is displayed
19 # (given as an RGB triplet or a Tk color string)
21 # - parent: which window to place the dialog on top of
23 # - title: dialog title
26 from tkCommonDialog
import Dialog
32 class Chooser(Dialog
):
35 command
= "tk_chooseColor"
37 def _fixoptions(self
):
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
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
51 return None, None # cancelled
52 r
, g
, b
= widget
.winfo_rgb(result
)
53 return (r
/256, g
/256, b
/256), result
59 def askcolor(color
= None, **options
):
63 options
= options
.copy()
64 options
["initialcolor"] = color
66 return apply(Chooser
, (), options
).show()
69 # --------------------------------------------------------------------
72 if __name__
== "__main__":
74 print "color", askcolor()