0001 # 0002 # Instant Python 0003 # $Id: tkColorChooser.py,v 1.6 2003/04/06 09:00:52 rhettinger Exp $ 0004 # 0005 # tk common colour chooser dialogue 0006 # 0007 # this module provides an interface to the native color dialogue 0008 # available in Tk 4.2 and newer. 0009 # 0010 # written by Fredrik Lundh, May 1997 0011 # 0012 # fixed initialcolor handling in August 1998 0013 # 0014 0015 # 0016 # options (all have default values): 0017 # 0018 # - initialcolor: colour to mark as selected when dialog is displayed 0019 # (given as an RGB triplet or a Tk color string) 0020 # 0021 # - parent: which window to place the dialog on top of 0022 # 0023 # - title: dialog title 0024 # 0025 0026 from tkCommonDialog import Dialog 0027 0028 0029 # 0030 # color chooser class 0031 0032 class Chooser(Dialog): 0033 "Ask for a color" 0034 0035 command = "tk_chooseColor" 0036 0037 def _fixoptions(self): 0038 try: 0039 # make sure initialcolor is a tk color string 0040 color = self.options["initialcolor"] 0041 if type(color) == type(()): 0042 # assume an RGB triplet 0043 self.options["initialcolor"] = "#%02x%02x%02x" % color 0044 except KeyError: 0045 pass 0046 0047 def _fixresult(self, widget, result): 0048 # to simplify application code, the color chooser returns 0049 # an RGB tuple together with the Tk color string 0050 if not result: 0051 return None, None # canceled 0052 r, g, b = widget.winfo_rgb(result) 0053 return (r/256, g/256, b/256), result 0054 0055 0056 # 0057 # convenience stuff 0058 0059 def askcolor(color = None, **options): 0060 "Ask for a color" 0061 0062 if color: 0063 options = options.copy() 0064 options["initialcolor"] = color 0065 0066 return Chooser(**options).show() 0067 0068 0069 # -------------------------------------------------------------------- 0070 # test stuff 0071 0072 if __name__ == "__main__": 0073 0074 print "color", askcolor() 0075
Generated by PyXR 0.9.4