blob: 64858d1a309a055dbab1bad73c1c1397614a4f44 [file] [log] [blame]
Guido van Rossum1e8c8a21997-07-19 20:02:36 +00001#
2# Instant Python
3# $Id$
4#
5# tk common colour chooser dialogue
6#
7# this module provides an interface to the native color dialogue
8# available in Tk 4.2 and newer.
9#
10# written by Fredrik Lundh, May 1997
11#
Guido van Rossum5ff17611998-08-07 14:55:21 +000012# fixed initialcolor handling in August 1998
13#
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000014
15#
16# options (all have default values):
17#
18# - initialcolor: colour to mark as selected when dialog is displayed
19# (given as an RGB triplet or a Tk color string)
20#
21# - parent: which window to place the dialog on top of
22#
23# - title: dialog title
24#
25
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000026from tkCommonDialog import Dialog
27
28
29#
30# color chooser class
31
32class Chooser(Dialog):
33 "Ask for a color"
34
35 command = "tk_chooseColor"
36
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
Guido van Rossum5ff17611998-08-07 14:55:21 +000043 self.options["initialcolor"] = "#%02x%02x%02x" % color
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000044 except KeyError:
45 pass
46
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:
Thomas Wouters7e474022000-07-16 12:04:32 +000051 return None, None # canceled
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000052 r, g, b = widget.winfo_rgb(result)
53 return (r/256, g/256, b/256), result
54
55
56#
57# convenience stuff
58
59def askcolor(color = None, **options):
60 "Ask for a color"
61
Guido van Rossum5ff17611998-08-07 14:55:21 +000062 if color:
Guido van Rossum2b427c71998-08-10 20:13:17 +000063 options = options.copy()
64 options["initialcolor"] = color
Guido van Rossum5ff17611998-08-07 14:55:21 +000065
Raymond Hettingerff41c482003-04-06 09:01:11 +000066 return Chooser(**options).show()
Guido van Rossum1e8c8a21997-07-19 20:02:36 +000067
68
69# --------------------------------------------------------------------
70# test stuff
71
72if __name__ == "__main__":
73
74 print "color", askcolor()