def __init__(self, master, pdb): self.pdb = pdb headings = tuple(pdb.fields) Tree.__init__(self, master, columns = headings, show = "headings", displaycolumns = headings) for i in range(len(headings)): self.heading("#%d" % (i+1), text = headings[i], command = lambda arg = headings[i]: self.fill(arg)) self.menu = Menu(self, tearoff = False) self.menu.add("command", compound = "left", ulabel = "_Add record", command = self.add, accelerator = "Insert") self.menu.add("command", compound = "left", ulabel = "E_dit record", command = self.edit, accelerator = "Alt-Insert") self.menu.add("command", compound = "left", ulabel = "_Delete record", command = self.delete, accelerator = "Delete") self.bind("<Button-3>", self.pop_menu) self.bind("<Key-App>", self.pop_menu) self.bind("<Key-Insert>", self.add) self.bind("<Key-Delete>", self.delete) self.bind("<Alt-Insert>", self.edit) self.editor = Frame(self, padding = 0, relief = "flat") self.editor.rowconfigure(0, weight = 1) i = 0 for name in self._columns(): w = TreeEntry(self.editor, name = name, padding = (3, 0), width = -1) w.grid(row = 0, column = i, sticky = "nesw") i += 1 for widget in self.editor.children.values(): widget.bind("<KeyPress-Return>", lambda event: self._hide_editor(True), add = "+") widget.bind("<KeyPress-Escape>", lambda event: self._hide_editor(), add = "+") self.fill(headings[0]) self.autosize()
def _hide_editor(self, save = False): self.unbind("<<TreeviewSelect>>") self.focus_displayof().update() item = getattr(self, "ITEM") if save: result = dict((name, widget.get()) for (name, widget) in self.editor.children.items()) record = self._2pdb(item) if record: self.pdb.update(record, **result) self.item(item, values = self._2tree(result)) else: id_ = self.pdb.insert(**result) self.pdb.update(self.pdb[id_]) self.item(item, tags = id_, values = self._2tree(result)) self.pdb.commit() if self.item(item, "tags") == "": Tree.delete(self, item) for widget in tuple(w for w in self.editor.children.values()): widget.clear() self.editor.place_forget() self.focus() delattr(self, "ITEM") delattr(self, "BBOX") self.unbind("<Configure>", getattr(self, "CNFEDIT")) delattr(self, "CNFEDIT")
def xview(self, *args): Tree.xview(self, *args) if self.editor.place_info() != {}: if args[0] == "scroll": self.update() self._adjust_editor() else: w = getattr(self, "BBOX")[2] x = max(0, float(args[-1])*w) dif = abs(w - self.winfo_width()) self.editor.place_configure(x = -(x if x <= dif else dif))
def yview(self, *args): if self.editor.place_info() == {}: Tree.yview(self, *args)
def delete(self, event = None): selection = self.selection() self.pdb.delete(self._2pdb(item) for item in selection) self.pdb.commit() Tree.delete(self, *selection)