Mercurial > cpython-lru_cache-weakref
annotate Tools/pynche/pyColorChooser.py @ 54294:dba767bc3155 legacy-trunk
remove mention of old ctypes version
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sat, 23 May 2009 20:59:09 +0000 |
| parents | ed0f0d96750c |
| children | c048a09d06ff |
| rev | line source |
|---|---|
|
9427
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
1 """Color chooser implementing (almost) the tkColorColor interface |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
2 """ |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
3 |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
4 import os |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
5 import Main |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
6 import ColorDB |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
7 |
| 18462 | 8 |
| 9 | |
|
9427
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
10 class Chooser: |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
11 """Ask for a color""" |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
12 def __init__(self, |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
13 master = None, |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
14 databasefile = None, |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
15 initfile = None, |
|
9428
1c94f34b49e3
Chooser.__init__(): Added `wantspec' keyword to conform exactly to
Barry Warsaw <barry@python.org>
parents:
9427
diff
changeset
|
16 ignore = None, |
|
1c94f34b49e3
Chooser.__init__(): Added `wantspec' keyword to conform exactly to
Barry Warsaw <barry@python.org>
parents:
9427
diff
changeset
|
17 wantspec = None): |
|
9427
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
18 self.__master = master |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
19 self.__databasefile = databasefile |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
20 self.__initfile = initfile or os.path.expanduser('~/.pynche') |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
21 self.__ignore = ignore |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
22 self.__pw = None |
|
9428
1c94f34b49e3
Chooser.__init__(): Added `wantspec' keyword to conform exactly to
Barry Warsaw <barry@python.org>
parents:
9427
diff
changeset
|
23 self.__wantspec = wantspec |
|
9427
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
24 |
|
10762
393a6daf289d
askcolor(): databasefile and wantspec can now be set on every
Barry Warsaw <barry@python.org>
parents:
10479
diff
changeset
|
25 def show(self, color, options): |
|
393a6daf289d
askcolor(): databasefile and wantspec can now be set on every
Barry Warsaw <barry@python.org>
parents:
10479
diff
changeset
|
26 # scan for options that can override the ctor options |
|
393a6daf289d
askcolor(): databasefile and wantspec can now be set on every
Barry Warsaw <barry@python.org>
parents:
10479
diff
changeset
|
27 self.__wantspec = options.get('wantspec', self.__wantspec) |
|
393a6daf289d
askcolor(): databasefile and wantspec can now be set on every
Barry Warsaw <barry@python.org>
parents:
10479
diff
changeset
|
28 dbfile = options.get('databasefile', self.__databasefile) |
|
393a6daf289d
askcolor(): databasefile and wantspec can now be set on every
Barry Warsaw <barry@python.org>
parents:
10479
diff
changeset
|
29 # load the database file |
|
393a6daf289d
askcolor(): databasefile and wantspec can now be set on every
Barry Warsaw <barry@python.org>
parents:
10479
diff
changeset
|
30 colordb = None |
|
393a6daf289d
askcolor(): databasefile and wantspec can now be set on every
Barry Warsaw <barry@python.org>
parents:
10479
diff
changeset
|
31 if dbfile <> self.__databasefile: |
|
393a6daf289d
askcolor(): databasefile and wantspec can now be set on every
Barry Warsaw <barry@python.org>
parents:
10479
diff
changeset
|
32 colordb = ColorDB.get_colordb(dbfile) |
|
9437
c6366893cee1
__init__(): Removed initialcolor keyword.
Barry Warsaw <barry@python.org>
parents:
9428
diff
changeset
|
33 if not self.__master: |
|
c6366893cee1
__init__(): Removed initialcolor keyword.
Barry Warsaw <barry@python.org>
parents:
9428
diff
changeset
|
34 from Tkinter import Tk |
|
c6366893cee1
__init__(): Removed initialcolor keyword.
Barry Warsaw <barry@python.org>
parents:
9428
diff
changeset
|
35 self.__master = Tk() |
|
9427
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
36 if not self.__pw: |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
37 self.__pw, self.__sb = \ |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
38 Main.build(master = self.__master, |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
39 initfile = self.__initfile, |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
40 ignore = self.__ignore) |
|
9437
c6366893cee1
__init__(): Removed initialcolor keyword.
Barry Warsaw <barry@python.org>
parents:
9428
diff
changeset
|
41 else: |
|
c6366893cee1
__init__(): Removed initialcolor keyword.
Barry Warsaw <barry@python.org>
parents:
9428
diff
changeset
|
42 self.__pw.deiconify() |
|
c6366893cee1
__init__(): Removed initialcolor keyword.
Barry Warsaw <barry@python.org>
parents:
9428
diff
changeset
|
43 # convert color |
|
10762
393a6daf289d
askcolor(): databasefile and wantspec can now be set on every
Barry Warsaw <barry@python.org>
parents:
10479
diff
changeset
|
44 if colordb: |
|
393a6daf289d
askcolor(): databasefile and wantspec can now be set on every
Barry Warsaw <barry@python.org>
parents:
10479
diff
changeset
|
45 self.__sb.set_colordb(colordb) |
|
393a6daf289d
askcolor(): databasefile and wantspec can now be set on every
Barry Warsaw <barry@python.org>
parents:
10479
diff
changeset
|
46 else: |
|
393a6daf289d
askcolor(): databasefile and wantspec can now be set on every
Barry Warsaw <barry@python.org>
parents:
10479
diff
changeset
|
47 colordb = self.__sb.colordb() |
|
9437
c6366893cee1
__init__(): Removed initialcolor keyword.
Barry Warsaw <barry@python.org>
parents:
9428
diff
changeset
|
48 if color: |
|
c6366893cee1
__init__(): Removed initialcolor keyword.
Barry Warsaw <barry@python.org>
parents:
9428
diff
changeset
|
49 r, g, b = Main.initial_color(color, colordb) |
|
c6366893cee1
__init__(): Removed initialcolor keyword.
Barry Warsaw <barry@python.org>
parents:
9428
diff
changeset
|
50 self.__sb.update_views(r, g, b) |
|
c6366893cee1
__init__(): Removed initialcolor keyword.
Barry Warsaw <barry@python.org>
parents:
9428
diff
changeset
|
51 # reset the canceled flag and run it |
|
c6366893cee1
__init__(): Removed initialcolor keyword.
Barry Warsaw <barry@python.org>
parents:
9428
diff
changeset
|
52 self.__sb.canceled(0) |
|
9427
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
53 Main.run(self.__pw, self.__sb) |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
54 rgbtuple = self.__sb.current_rgb() |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
55 self.__pw.withdraw() |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
56 # check to see if the cancel button was pushed |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
57 if self.__sb.canceled_p(): |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
58 return None, None |
| 18462 | 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 | |
|
9427
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
61 # aliases for now, maybe later we should return these too. |
|
9428
1c94f34b49e3
Chooser.__init__(): Added `wantspec' keyword to conform exactly to
Barry Warsaw <barry@python.org>
parents:
9427
diff
changeset
|
62 name = None |
|
1c94f34b49e3
Chooser.__init__(): Added `wantspec' keyword to conform exactly to
Barry Warsaw <barry@python.org>
parents:
9427
diff
changeset
|
63 if not self.__wantspec: |
|
1c94f34b49e3
Chooser.__init__(): Added `wantspec' keyword to conform exactly to
Barry Warsaw <barry@python.org>
parents:
9427
diff
changeset
|
64 try: |
|
1c94f34b49e3
Chooser.__init__(): Added `wantspec' keyword to conform exactly to
Barry Warsaw <barry@python.org>
parents:
9427
diff
changeset
|
65 name = colordb.find_byrgb(rgbtuple)[0] |
|
1c94f34b49e3
Chooser.__init__(): Added `wantspec' keyword to conform exactly to
Barry Warsaw <barry@python.org>
parents:
9427
diff
changeset
|
66 except ColorDB.BadColor: |
|
1c94f34b49e3
Chooser.__init__(): Added `wantspec' keyword to conform exactly to
Barry Warsaw <barry@python.org>
parents:
9427
diff
changeset
|
67 pass |
|
1c94f34b49e3
Chooser.__init__(): Added `wantspec' keyword to conform exactly to
Barry Warsaw <barry@python.org>
parents:
9427
diff
changeset
|
68 if name is None: |
|
9427
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
69 name = ColorDB.triplet_to_rrggbb(rgbtuple) |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
70 return rgbtuple, name |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
71 |
|
9437
c6366893cee1
__init__(): Removed initialcolor keyword.
Barry Warsaw <barry@python.org>
parents:
9428
diff
changeset
|
72 def save(self): |
|
c6366893cee1
__init__(): Removed initialcolor keyword.
Barry Warsaw <barry@python.org>
parents:
9428
diff
changeset
|
73 if self.__sb: |
|
c6366893cee1
__init__(): Removed initialcolor keyword.
Barry Warsaw <barry@python.org>
parents:
9428
diff
changeset
|
74 self.__sb.save_views() |
|
9427
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
75 |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
76 |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
77 # convenience stuff |
|
9437
c6366893cee1
__init__(): Removed initialcolor keyword.
Barry Warsaw <barry@python.org>
parents:
9428
diff
changeset
|
78 _chooser = None |
|
c6366893cee1
__init__(): Removed initialcolor keyword.
Barry Warsaw <barry@python.org>
parents:
9428
diff
changeset
|
79 |
|
9427
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
80 def askcolor(color = None, **options): |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
81 """Ask for a color""" |
|
9437
c6366893cee1
__init__(): Removed initialcolor keyword.
Barry Warsaw <barry@python.org>
parents:
9428
diff
changeset
|
82 global _chooser |
|
c6366893cee1
__init__(): Removed initialcolor keyword.
Barry Warsaw <barry@python.org>
parents:
9428
diff
changeset
|
83 if not _chooser: |
|
c6366893cee1
__init__(): Removed initialcolor keyword.
Barry Warsaw <barry@python.org>
parents:
9428
diff
changeset
|
84 _chooser = apply(Chooser, (), options) |
|
10762
393a6daf289d
askcolor(): databasefile and wantspec can now be set on every
Barry Warsaw <barry@python.org>
parents:
10479
diff
changeset
|
85 return _chooser.show(color, options) |
|
9427
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
86 |
|
9437
c6366893cee1
__init__(): Removed initialcolor keyword.
Barry Warsaw <barry@python.org>
parents:
9428
diff
changeset
|
87 def save(): |
|
c6366893cee1
__init__(): Removed initialcolor keyword.
Barry Warsaw <barry@python.org>
parents:
9428
diff
changeset
|
88 global _chooser |
|
c6366893cee1
__init__(): Removed initialcolor keyword.
Barry Warsaw <barry@python.org>
parents:
9428
diff
changeset
|
89 if _chooser: |
|
c6366893cee1
__init__(): Removed initialcolor keyword.
Barry Warsaw <barry@python.org>
parents:
9428
diff
changeset
|
90 _chooser.save() |
|
9427
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
91 |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
92 |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
93 # test stuff |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
94 if __name__ == '__main__': |
|
16396
b22e4dca451a
Move the "from Tkinter import *" out of the method and into the module
Barry Warsaw <barry@python.org>
parents:
10762
diff
changeset
|
95 from Tkinter import * |
|
b22e4dca451a
Move the "from Tkinter import *" out of the method and into the module
Barry Warsaw <barry@python.org>
parents:
10762
diff
changeset
|
96 |
|
9427
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
97 class Tester: |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
98 def __init__(self): |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
99 self.__root = tk = Tk() |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
100 b = Button(tk, text='Choose Color...', command=self.__choose) |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
101 b.pack() |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
102 self.__l = Label(tk) |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
103 self.__l.pack() |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
104 q = Button(tk, text='Quit', command=self.__quit) |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
105 q.pack() |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
106 |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
107 def __choose(self, event=None): |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
108 rgb, name = askcolor(master=self.__root) |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
109 if rgb is None: |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
110 text = 'You hit CANCEL!' |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
111 else: |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
112 r, g, b = rgb |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
113 text = 'You picked %s (%3d/%3d/%3d)' % (name, r, g, b) |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
114 self.__l.configure(text=text) |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
115 |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
116 def __quit(self, event=None): |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
117 self.__root.quit() |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
118 |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
119 def run(self): |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
120 self.__root.mainloop() |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
121 t = Tester() |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
122 t.run() |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
123 # simpler |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
124 ## print 'color:', askcolor() |
|
7567ef40de77
Many changes to support a second mode of operation. Pynche can now be
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
125 ## print 'color:', askcolor() |
