annotate Lib/lib-tk/tkColorChooser.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 787b969d37f0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # tk common colour chooser dialogue
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 #
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # this module provides an interface to the native color dialogue
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 # available in Tk 4.2 and newer.
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 #
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 # written by Fredrik Lundh, May 1997
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 #
8858
567607076d8f From: "Fredrik Lundh" <fredrik@pythonware.com>
Guido van Rossum <guido@python.org>
parents: 6013
diff changeset
8 # fixed initialcolor handling in August 1998
567607076d8f From: "Fredrik Lundh" <fredrik@pythonware.com>
Guido van Rossum <guido@python.org>
parents: 6013
diff changeset
9 #
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 #
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 # options (all have default values):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 #
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 # - initialcolor: colour to mark as selected when dialog is displayed
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 # (given as an RGB triplet or a Tk color string)
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 #
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 # - parent: which window to place the dialog on top of
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 #
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 # - title: dialog title
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 #
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 from tkCommonDialog import Dialog
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
23
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 #
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 # color chooser class
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 class Chooser(Dialog):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 "Ask for a color"
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
30
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 command = "tk_chooseColor"
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 def _fixoptions(self):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 try:
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 # make sure initialcolor is a tk color string
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 color = self.options["initialcolor"]
50148
3a332bf06da0 Issue #3767: Convert Tk object to string in tkColorChooser.
Martin v. Löwis <martin@v.loewis.de>
parents: 35858
diff changeset
37 if isinstance(color, tuple):
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 # assume an RGB triplet
8858
567607076d8f From: "Fredrik Lundh" <fredrik@pythonware.com>
Guido van Rossum <guido@python.org>
parents: 6013
diff changeset
39 self.options["initialcolor"] = "#%02x%02x%02x" % color
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 except KeyError:
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 pass
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
42
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 def _fixresult(self, widget, result):
50148
3a332bf06da0 Issue #3767: Convert Tk object to string in tkColorChooser.
Martin v. Löwis <martin@v.loewis.de>
parents: 35858
diff changeset
44 # result can be somethings: an empty tuple, an empty string or
3a332bf06da0 Issue #3767: Convert Tk object to string in tkColorChooser.
Martin v. Löwis <martin@v.loewis.de>
parents: 35858
diff changeset
45 # a Tcl_Obj, so this somewhat weird check handles that
3a332bf06da0 Issue #3767: Convert Tk object to string in tkColorChooser.
Martin v. Löwis <martin@v.loewis.de>
parents: 35858
diff changeset
46 if not result or not str(result):
3a332bf06da0 Issue #3767: Convert Tk object to string in tkColorChooser.
Martin v. Löwis <martin@v.loewis.de>
parents: 35858
diff changeset
47 return None, None # canceled
3a332bf06da0 Issue #3767: Convert Tk object to string in tkColorChooser.
Martin v. Löwis <martin@v.loewis.de>
parents: 35858
diff changeset
48
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 # to simplify application code, the color chooser returns
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 # an RGB tuple together with the Tk color string
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 r, g, b = widget.winfo_rgb(result)
50148
3a332bf06da0 Issue #3767: Convert Tk object to string in tkColorChooser.
Martin v. Löwis <martin@v.loewis.de>
parents: 35858
diff changeset
52 return (r/256, g/256, b/256), str(result)
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
54
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 #
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 # convenience stuff
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 def askcolor(color = None, **options):
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 "Ask for a color"
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
60
8858
567607076d8f From: "Fredrik Lundh" <fredrik@pythonware.com>
Guido van Rossum <guido@python.org>
parents: 6013
diff changeset
61 if color:
8922
8e41906863be Nannified.
Guido van Rossum <guido@python.org>
parents: 8858
diff changeset
62 options = options.copy()
8e41906863be Nannified.
Guido van Rossum <guido@python.org>
parents: 8858
diff changeset
63 options["initialcolor"] = color
8858
567607076d8f From: "Fredrik Lundh" <fredrik@pythonware.com>
Guido van Rossum <guido@python.org>
parents: 6013
diff changeset
64
28513
90c77b8af1a4 SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents: 13620
diff changeset
65 return Chooser(**options).show()
5869
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
67
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 # --------------------------------------------------------------------
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 # test stuff
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
70
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 if __name__ == "__main__":
f2bf7bcf8b8b New dialog routines (Fred Lundh)
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 print "color", askcolor()