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
28
class Chooser(Dialog):
31
command = "tk_chooseColor"
33
def _fixoptions(self):
35
# make sure initialcolor is a tk color string
36
color = self.options["initialcolor"]
37
if type(color) == type(()):
38
# assume an RGB triplet
39
self.options["initialcolor"] = "#%02x%02x%02x" % color
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
47
return None, None # canceled
48
r, g, b = widget.winfo_rgb(result)
49
return (r/256, g/256, b/256), result
55
def askcolor(color = None, **options):
59
options = options.copy()
60
options["initialcolor"] = color
62
return Chooser(**options).show()
65
# --------------------------------------------------------------------
68
if __name__ == "__main__":
70
print "color", askcolor()