~malept/ubuntu/lucid/python2.6/dev-dependency-fix

« back to all changes in this revision

Viewing changes to Lib/lib-tk/tkColorChooser.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-02-13 12:51:00 UTC
  • Revision ID: james.westby@ubuntu.com-20090213125100-uufgcb9yeqzujpqw
Tags: upstream-2.6.1
ImportĀ upstreamĀ versionĀ 2.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# tk common colour chooser dialogue
 
2
#
 
3
# this module provides an interface to the native color dialogue
 
4
# available in Tk 4.2 and newer.
 
5
#
 
6
# written by Fredrik Lundh, May 1997
 
7
#
 
8
# fixed initialcolor handling in August 1998
 
9
#
 
10
 
 
11
#
 
12
# options (all have default values):
 
13
#
 
14
# - initialcolor: colour to mark as selected when dialog is displayed
 
15
#   (given as an RGB triplet or a Tk color string)
 
16
#
 
17
# - parent: which window to place the dialog on top of
 
18
#
 
19
# - title: dialog title
 
20
#
 
21
 
 
22
from tkCommonDialog import Dialog
 
23
 
 
24
 
 
25
#
 
26
# color chooser class
 
27
 
 
28
class Chooser(Dialog):
 
29
    "Ask for a color"
 
30
 
 
31
    command = "tk_chooseColor"
 
32
 
 
33
    def _fixoptions(self):
 
34
        try:
 
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
 
40
        except KeyError:
 
41
            pass
 
42
 
 
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
 
46
        if not result:
 
47
            return None, None # canceled
 
48
        r, g, b = widget.winfo_rgb(result)
 
49
        return (r/256, g/256, b/256), result
 
50
 
 
51
 
 
52
#
 
53
# convenience stuff
 
54
 
 
55
def askcolor(color = None, **options):
 
56
    "Ask for a color"
 
57
 
 
58
    if color:
 
59
        options = options.copy()
 
60
        options["initialcolor"] = color
 
61
 
 
62
    return Chooser(**options).show()
 
63
 
 
64
 
 
65
# --------------------------------------------------------------------
 
66
# test stuff
 
67
 
 
68
if __name__ == "__main__":
 
69
 
 
70
    print "color", askcolor()