Added 'list_only' option (and modified 'run()' to respect it).
[python/dscho.git] / Lib / lib-tk / tkColorChooser.py
blob5756275732bf1bd4726ab50d8fbbd080ce603feb
2 # Instant Python
3 # $Id$
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
30 # color chooser class
32 class Chooser(Dialog):
33 "Ask for a color"
35 command = "tk_chooseColor"
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
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 # cancelled
52 r, g, b = widget.winfo_rgb(result)
53 return (r/256, g/256, b/256), result
57 # convenience stuff
59 def askcolor(color = None, **options):
60 "Ask for a color"
62 if color:
63 options = options.copy()
64 options["initialcolor"] = color
66 return apply(Chooser, (), options).show()
69 # --------------------------------------------------------------------
70 # test stuff
72 if __name__ == "__main__":
74 print "color", askcolor()