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