Advertisement
wellidkk

tkinter filetree

Apr 4th, 2023
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.58 KB | None | 0 0
  1. b'\nimport tkinter as tk\nimport tkinter.ttk as ttk\n\nclass CheckboxTreeview(ttk.Treeview):\n    """\n        Treeview widget with checkboxes left of each item.\n        The checkboxes are done via the image attribute of the item, so to keep\n        the checkbox, you cannot add an image to the item.\n    """\n\n    def __init__(self, master=None, **kw):\n        ttk.Treeview.__init__(self, master, **kw)\n        # checkboxes are implemented with pictures\n        self.im_checked = tk.PhotoImage(file=\'checked.png\')\n        self.im_unchecked = tk.PhotoImage(file=\'unchecked.png\')\n        self.im_tristate = tk.PhotoImage(file=\'tristate.png\')\n        self.tag_configure("unchecked", image=self.im_unchecked)\n        self.tag_configure("tristate", image=self.im_tristate)\n        self.tag_configure("checked", image=self.im_checked)\n        # check / uncheck boxes on click\n        self.bind("<Button-1>", self.box_click, True)\n\n    def insert(self, parent, index, iid=None, **kw):\n        """ same method as for standard treeview but add the tag \'unchecked\'\n            automatically if no tag among (\'checked\', \'unchecked\', \'tristate\')\n            is given """\n        if not "tags" in kw:\n            kw["tags"] = ("unchecked",)\n        elif not ("unchecked" in kw["tags"] or "checked" in kw["tags"]\n                  or "tristate" in kw["tags"]):\n            kw["tags"] = ("unchecked",)\n        ttk.Treeview.insert(self, parent, index, iid, **kw)\n\n    def check_descendant(self, item):\n        """ check the boxes of item\'s descendants """\n        children = self.get_children(item)\n        for iid in children:\n            self.item(iid, tags=("checked",))\n            self.check_descendant(iid)\n\n    def check_ancestor(self, item):\n        """ check the box of item and change the state of the boxes of item\'s\n            ancestors accordingly """\n        self.item(item, tags=("checked",))\n        parent = self.parent(item)\n        if parent:\n            children = self.get_children(parent)\n            b = ["checked" in self.item(c, "tags") for c in children]\n            if False in b:\n                # at least one box is not checked and item\'s box is checked\n                self.tristate_parent(parent)\n            else:\n                # all boxes of the children are checked\n                self.check_ancestor(parent)\n\n    def tristate_parent(self, item):\n        """ put the box of item in tristate and change the state of the boxes of\n            item\'s ancestors accordingly """\n        self.item(item, tags=("tristate",))\n        parent = self.parent(item)\n        if parent:\n            self.tristate_parent(parent)\n\n    def uncheck_descendant(self, item):\n        """ uncheck the boxes of item\'s descendant """\n        children = self.get_children(item)\n        for iid in children:\n            self.item(iid, tags=("unchecked",))\n            self.uncheck_descendant(iid)\n\n    def uncheck_ancestor(self, item):\n        """ uncheck the box of item and change the state of the boxes of item\'s\n            ancestors accordingly """\n        self.item(item, tags=("unchecked",))\n        parent = self.parent(item)\n        if parent:\n            children = self.get_children(parent)\n            b = ["unchecked" in self.item(c, "tags") for c in children]\n            if False in b:\n                # at least one box is checked and item\'s box is unchecked\n                self.tristate_parent(parent)\n            else:\n                # no box is checked\n                self.uncheck_ancestor(parent)\n\n    def box_click(self, event):\n        """ check or uncheck box when clicked """\n        x, y, widget = event.x, event.y, event.widget\n        elem = widget.identify("element", x, y)\n        if "image" in elem:\n            # a box was clicked\n            item = self.identify_row(y)\n            tags = self.item(item, "tags")\n            if ("unchecked" in tags) or ("tristate" in tags):\n                self.check_ancestor(item)\n                self.check_descendant(item)\n            else:\n                self.uncheck_descendant(item)\n                self.uncheck_ancestor(item)\n\n\n\nif __name__ == \'__main__\':\n    root = tk.Tk()\n    t = CheckboxTreeview(root, show="tree")\n    t.pack()\n    t.insert("", 0, "1", text="1")\n    t.insert("1", "end", "11", text="1")\n    t.insert("1", "end", "12", text="2")\n    t.insert("12", "end", "121", text="1")\n    t.insert("12", "end", "122", text="2")\n    t.insert("122", "end", "1221", text="1")\n    t.insert("1", "end", "13", text="3")\n    t.insert("13", "end", "131", text="1")\n    root.mainloop()'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement