ยปCore Development>Code coverage>Tools/pynche/pyColorChooser.py

Python code coverage for Tools/pynche/pyColorChooser.py

#countcontent
1n/a"""Color chooser implementing (almost) the tkColorColor interface
2n/a"""
3n/a
4n/aimport os
5n/aimport Main
6n/aimport ColorDB
7n/a
8n/a
9n/a
10n/aclass Chooser:
11n/a """Ask for a color"""
12n/a def __init__(self,
13n/a master = None,
14n/a databasefile = None,
15n/a initfile = None,
16n/a ignore = None,
17n/a wantspec = None):
18n/a self.__master = master
19n/a self.__databasefile = databasefile
20n/a self.__initfile = initfile or os.path.expanduser('~/.pynche')
21n/a self.__ignore = ignore
22n/a self.__pw = None
23n/a self.__wantspec = wantspec
24n/a
25n/a def show(self, color, options):
26n/a # scan for options that can override the ctor options
27n/a self.__wantspec = options.get('wantspec', self.__wantspec)
28n/a dbfile = options.get('databasefile', self.__databasefile)
29n/a # load the database file
30n/a colordb = None
31n/a if dbfile != self.__databasefile:
32n/a colordb = ColorDB.get_colordb(dbfile)
33n/a if not self.__master:
34n/a from tkinter import Tk
35n/a self.__master = Tk()
36n/a if not self.__pw:
37n/a self.__pw, self.__sb = \
38n/a Main.build(master = self.__master,
39n/a initfile = self.__initfile,
40n/a ignore = self.__ignore)
41n/a else:
42n/a self.__pw.deiconify()
43n/a # convert color
44n/a if colordb:
45n/a self.__sb.set_colordb(colordb)
46n/a else:
47n/a colordb = self.__sb.colordb()
48n/a if color:
49n/a r, g, b = Main.initial_color(color, colordb)
50n/a self.__sb.update_views(r, g, b)
51n/a # reset the canceled flag and run it
52n/a self.__sb.canceled(0)
53n/a Main.run(self.__pw, self.__sb)
54n/a rgbtuple = self.__sb.current_rgb()
55n/a self.__pw.withdraw()
56n/a # check to see if the cancel button was pushed
57n/a if self.__sb.canceled_p():
58n/a return None, None
59n/a # Try to return the color name from the database if there is an exact
60n/a # match, otherwise use the "#rrggbb" spec. BAW: Forget about color
61n/a # aliases for now, maybe later we should return these too.
62n/a name = None
63n/a if not self.__wantspec:
64n/a try:
65n/a name = colordb.find_byrgb(rgbtuple)[0]
66n/a except ColorDB.BadColor:
67n/a pass
68n/a if name is None:
69n/a name = ColorDB.triplet_to_rrggbb(rgbtuple)
70n/a return rgbtuple, name
71n/a
72n/a def save(self):
73n/a if self.__sb:
74n/a self.__sb.save_views()
75n/a
76n/a
77n/a# convenience stuff
78n/a_chooser = None
79n/a
80n/adef askcolor(color = None, **options):
81n/a """Ask for a color"""
82n/a global _chooser
83n/a if not _chooser:
84n/a _chooser = Chooser(**options)
85n/a return _chooser.show(color, options)
86n/a
87n/adef save():
88n/a global _chooser
89n/a if _chooser:
90n/a _chooser.save()
91n/a
92n/a
93n/a# test stuff
94n/aif __name__ == '__main__':
95n/a from tkinter import *
96n/a
97n/a class Tester:
98n/a def __init__(self):
99n/a self.__root = tk = Tk()
100n/a b = Button(tk, text='Choose Color...', command=self.__choose)
101n/a b.pack()
102n/a self.__l = Label(tk)
103n/a self.__l.pack()
104n/a q = Button(tk, text='Quit', command=self.__quit)
105n/a q.pack()
106n/a
107n/a def __choose(self, event=None):
108n/a rgb, name = askcolor(master=self.__root)
109n/a if rgb is None:
110n/a text = 'You hit CANCEL!'
111n/a else:
112n/a r, g, b = rgb
113n/a text = 'You picked %s (%3d/%3d/%3d)' % (name, r, g, b)
114n/a self.__l.configure(text=text)
115n/a
116n/a def __quit(self, event=None):
117n/a self.__root.quit()
118n/a
119n/a def run(self):
120n/a self.__root.mainloop()
121n/a t = Tester()
122n/a t.run()
123n/a # simpler
124n/a## print 'color:', askcolor()
125n/a## print 'color:', askcolor()