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