Suppress transient refleaks in test_asyncore
[python.git] / Lib / lib-tk / tkColorChooser.py
blobcf6283b357223185d725e35c1a56d2154593592b
1 # tk common colour chooser dialogue
3 # this module provides an interface to the native color dialogue
4 # available in Tk 4.2 and newer.
6 # written by Fredrik Lundh, May 1997
8 # fixed initialcolor handling in August 1998
12 # options (all have default values):
14 # - initialcolor: colour to mark as selected when dialog is displayed
15 # (given as an RGB triplet or a Tk color string)
17 # - parent: which window to place the dialog on top of
19 # - title: dialog title
22 from tkCommonDialog import Dialog
26 # color chooser class
28 class Chooser(Dialog):
29 "Ask for a color"
31 command = "tk_chooseColor"
33 def _fixoptions(self):
34 try:
35 # make sure initialcolor is a tk color string
36 color = self.options["initialcolor"]
37 if isinstance(color, tuple):
38 # assume an RGB triplet
39 self.options["initialcolor"] = "#%02x%02x%02x" % color
40 except KeyError:
41 pass
43 def _fixresult(self, widget, result):
44 # result can be somethings: an empty tuple, an empty string or
45 # a Tcl_Obj, so this somewhat weird check handles that
46 if not result or not str(result):
47 return None, None # canceled
49 # to simplify application code, the color chooser returns
50 # an RGB tuple together with the Tk color string
51 r, g, b = widget.winfo_rgb(result)
52 return (r/256, g/256, b/256), str(result)
56 # convenience stuff
58 def askcolor(color = None, **options):
59 "Ask for a color"
61 if color:
62 options = options.copy()
63 options["initialcolor"] = color
65 return Chooser(**options).show()
68 # --------------------------------------------------------------------
69 # test stuff
71 if __name__ == "__main__":
72 print "color", askcolor()