def __init__(self, window): window.title("Reference list") self.list = Tree( window, tree=False, columns=( dict(heading="Origin", width=1), dict(heading="Name", width=20, stretch=True), dict(heading="Authority", width=6), dict(heading="Common name", width=15), dict(heading="Family", width=(3, Tree.FIGURE)), dict(heading="Family", width=8), dict(heading="Family", width=6), dict(heading="Division", width=(1, Tree.FIGURE)), dict(heading="Division", width=6), dict(heading="Note", width=3), dict(heading="SPECNUM", width=(4, Tree.FIGURE)), ), ) scroll(self.list, resize=True) self.list.focus_set() self.items = dict() self.records = 0
class Ui(object): def __init__(self, window): window.title("Reference list") self.list = Tree( window, tree=False, columns=( dict(heading="Origin", width=1), dict(heading="Name", width=20, stretch=True), dict(heading="Authority", width=6), dict(heading="Common name", width=15), dict(heading="Family", width=(3, Tree.FIGURE)), dict(heading="Family", width=8), dict(heading="Family", width=6), dict(heading="Division", width=(1, Tree.FIGURE)), dict(heading="Division", width=6), dict(heading="Note", width=3), dict(heading="SPECNUM", width=(4, Tree.FIGURE)), ), ) scroll(self.list, resize=True) self.list.focus_set() self.items = dict() self.records = 0 def add_files(self, Reader, files, convert=attrgetter("__dict__")): for file in files: print("Reading", file, file=stderr) with closing(Reader(file)) as file: for plant in file: self.add_plant(convert(plant)) self.print_count() print(file=stderr) def add_plant(self, plant): fields = ( "origin", "name", "auth", "common", "famnum", "family", "fam_com", "divnum", "group", "note", "specnum", ) key = plant_key(plant["name"]) try: item = self.items[key] except LookupError: item = self.list.add() self.items[key] = item current = self.list.item(item, option="values") current = tuple_record(current, fields, ("origin",)) if getattr(current, "origin", "?") == "?": current.origin = None for field in fields: if getattr(current, field, None) is None: value = plant.get(field) if value is None: if field == "origin": value = "?" else: value = "" setattr(current, field, value) self.list.item(item, values=tuple(getattr(current, field) for field in fields)) self.records += 1 if not self.records % 200: self.print_count() def print_count(self): print("Records:", self.records, "Plants:", len(self.items), end="\r", file=stderr) def entry_changed(self, value): patterns = value.translate(SearchMap()).split() self.matches.delete(*self.matches.get_children()) if patterns: for match in name_matches(self.names, patterns): s = str() i = 0 space = True for word in match.split(): if not space: s += " " while i < len(value): c = value[i] i += 1 if c.isalnum() or c == "_": break s += c s += word end = i space = False while i < len(value): c = value[i] i += 1 if c.isalnum() or c == "_": end = i elif c.isspace(): space = True break s += value[end:i] s += value[i:] self.matches.add(values=(s,)) return True def select_match(self, dir, event): focus = self.matches.focus() if (focus,) == self.matches.selection(): new = getattr(self.matches, dir)(focus) if new: focus = new if not focus: return self.matches.see(focus) self.matches.focus(focus) self.matches.selection_set((focus,)) self.entry.configure(validate="none") self.entry.delete(0, tkinter.END) self.entry.insert(0, self.matches.set(focus, 0)) self.entry.select_range(0, tkinter.END) self.entry.configure(validate="key") def add_entry(self, event): print(self.entry.get()) self.entry.delete(0, tkinter.END)