ยปCore Development>Code coverage>Lib/tkinter/colorchooser.py

Python code coverage for Lib/tkinter/colorchooser.py

#countcontent
1n/a# tk common color chooser dialogue
2n/a#
3n/a# this module provides an interface to the native color dialogue
4n/a# available in Tk 4.2 and newer.
5n/a#
6n/a# written by Fredrik Lundh, May 1997
7n/a#
8n/a# fixed initialcolor handling in August 1998
9n/a#
10n/a
11n/a#
12n/a# options (all have default values):
13n/a#
14n/a# - initialcolor: color to mark as selected when dialog is displayed
15n/a# (given as an RGB triplet or a Tk color string)
16n/a#
17n/a# - parent: which window to place the dialog on top of
18n/a#
19n/a# - title: dialog title
20n/a#
21n/a
22n/afrom tkinter.commondialog import Dialog
23n/a
24n/a
25n/a#
26n/a# color chooser class
27n/a
28n/aclass Chooser(Dialog):
29n/a "Ask for a color"
30n/a
31n/a command = "tk_chooseColor"
32n/a
33n/a def _fixoptions(self):
34n/a try:
35n/a # make sure initialcolor is a tk color string
36n/a color = self.options["initialcolor"]
37n/a if isinstance(color, tuple):
38n/a # assume an RGB triplet
39n/a self.options["initialcolor"] = "#%02x%02x%02x" % color
40n/a except KeyError:
41n/a pass
42n/a
43n/a def _fixresult(self, widget, result):
44n/a # result can be somethings: an empty tuple, an empty string or
45n/a # a Tcl_Obj, so this somewhat weird check handles that
46n/a if not result or not str(result):
47n/a return None, None # canceled
48n/a
49n/a # to simplify application code, the color chooser returns
50n/a # an RGB tuple together with the Tk color string
51n/a r, g, b = widget.winfo_rgb(result)
52n/a return (r/256, g/256, b/256), str(result)
53n/a
54n/a
55n/a#
56n/a# convenience stuff
57n/a
58n/adef askcolor(color = None, **options):
59n/a "Ask for a color"
60n/a
61n/a if color:
62n/a options = options.copy()
63n/a options["initialcolor"] = color
64n/a
65n/a return Chooser(**options).show()
66n/a
67n/a
68n/a# --------------------------------------------------------------------
69n/a# test stuff
70n/a
71n/aif __name__ == "__main__":
72n/a print("color", askcolor())