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