1 """Color chooser implementing (almost) the tkColorColor interface
16 self
.__master
= master
17 self
.__databasefile
= databasefile
18 self
.__initfile
= initfile
or os
.path
.expanduser('~/.pynche')
19 self
.__ignore
= ignore
21 self
.__wantspec
= wantspec
23 def show(self
, color
, options
):
24 # scan for options that can override the ctor options
25 self
.__wantspec
= options
.get('wantspec', self
.__wantspec
)
26 dbfile
= options
.get('databasefile', self
.__databasefile
)
27 # load the database file
29 if dbfile
<> self
.__databasefile
:
30 colordb
= ColorDB
.get_colordb(dbfile
)
32 from Tkinter
import Tk
35 self
.__pw
, self
.__sb
= \
36 Main
.build(master
= self
.__master
,
37 initfile
= self
.__initfile
,
38 ignore
= self
.__ignore
)
43 self
.__sb
.set_colordb(colordb
)
45 colordb
= self
.__sb
.colordb()
47 r
, g
, b
= Main
.initial_color(color
, colordb
)
48 self
.__sb
.update_views(r
, g
, b
)
49 # reset the canceled flag and run it
51 Main
.run(self
.__pw
, self
.__sb
)
52 rgbtuple
= self
.__sb
.current_rgb()
54 # check to see if the cancel button was pushed
55 if self
.__sb
.canceled_p():
57 # try to return the color name from the database if there is an exact
58 # match, otherwise use the "#rrggbb" spec. TBD: Forget about color
59 # aliases for now, maybe later we should return these too.
61 if not self
.__wantspec
:
63 name
= colordb
.find_byrgb(rgbtuple
)[0]
64 except ColorDB
.BadColor
:
67 name
= ColorDB
.triplet_to_rrggbb(rgbtuple
)
72 self
.__sb
.save_views()
78 def askcolor(color
= None, **options
):
82 _chooser
= apply(Chooser
, (), options
)
83 return _chooser
.show(color
, options
)
92 if __name__
== '__main__':
97 self
.__root
= tk
= Tk()
98 b
= Button(tk
, text
='Choose Color...', command
=self
.__choose
)
102 q
= Button(tk
, text
='Quit', command
=self
.__quit
)
105 def __choose(self
, event
=None):
106 rgb
, name
= askcolor(master
=self
.__root
)
108 text
= 'You hit CANCEL!'
111 text
= 'You picked %s (%3d/%3d/%3d)' % (name
, r
, g
, b
)
112 self
.__l.configure(text
=text
)
114 def __quit(self
, event
=None):
118 self
.__root
.mainloop()
122 ## print 'color:', askcolor()
123 ## print 'color:', askcolor()