1 """Color chooser implementing (almost) the tkColorColor interface
18 self
.__master
= master
19 self
.__databasefile
= databasefile
20 self
.__initfile
= initfile
or os
.path
.expanduser('~/.pynche')
21 self
.__ignore
= ignore
23 self
.__wantspec
= wantspec
25 def show(self
, color
, options
):
26 # scan for options that can override the ctor options
27 self
.__wantspec
= options
.get('wantspec', self
.__wantspec
)
28 dbfile
= options
.get('databasefile', self
.__databasefile
)
29 # load the database file
31 if dbfile
<> self
.__databasefile
:
32 colordb
= ColorDB
.get_colordb(dbfile
)
34 from Tkinter
import Tk
37 self
.__pw
, self
.__sb
= \
38 Main
.build(master
= self
.__master
,
39 initfile
= self
.__initfile
,
40 ignore
= self
.__ignore
)
45 self
.__sb
.set_colordb(colordb
)
47 colordb
= self
.__sb
.colordb()
49 r
, g
, b
= Main
.initial_color(color
, colordb
)
50 self
.__sb
.update_views(r
, g
, b
)
51 # reset the canceled flag and run it
53 Main
.run(self
.__pw
, self
.__sb
)
54 rgbtuple
= self
.__sb
.current_rgb()
56 # check to see if the cancel button was pushed
57 if self
.__sb
.canceled_p():
59 # Try to return the color name from the database if there is an exact
60 # match, otherwise use the "#rrggbb" spec. BAW: Forget about color
61 # aliases for now, maybe later we should return these too.
63 if not self
.__wantspec
:
65 name
= colordb
.find_byrgb(rgbtuple
)[0]
66 except ColorDB
.BadColor
:
69 name
= ColorDB
.triplet_to_rrggbb(rgbtuple
)
74 self
.__sb
.save_views()
80 def askcolor(color
= None, **options
):
84 _chooser
= apply(Chooser
, (), options
)
85 return _chooser
.show(color
, options
)
94 if __name__
== '__main__':
99 self
.__root
= tk
= Tk()
100 b
= Button(tk
, text
='Choose Color...', command
=self
.__choose
)
104 q
= Button(tk
, text
='Quit', command
=self
.__quit
)
107 def __choose(self
, event
=None):
108 rgb
, name
= askcolor(master
=self
.__root
)
110 text
= 'You hit CANCEL!'
113 text
= 'You picked %s (%3d/%3d/%3d)' % (name
, r
, g
, b
)
114 self
.__l.configure(text
=text
)
116 def __quit(self
, event
=None):
120 self
.__root
.mainloop()
124 ## print 'color:', askcolor()
125 ## print 'color:', askcolor()