使用 Python 在 tkinter 中创建分层复选框树视图

Creating Hierarchical checkbox tree view in tkinter using Python

import tkinter.tix as Tix

class View(object):
    def __init__(self, root):
        self.root = root
        self.makeCheckList()

    def makeCheckList(self):
        self.cl = Tix.CheckList(self.root,height=200,width=400)
        self.cl.pack()
        self.cl.hlist.add("C", text="GeeksforGeeks")
        self.cl.hlist.add("C.CL1", text="Computer Science")
        self.cl.hlist.add("C.CL1.Item1", text="Algorithm")
        self.cl.hlist.add("C.CL1.Item2", text="Data Structures")
        self.cl.hlist.add("C.CL2", text="Gate Paper")
        self.cl.hlist.add("C.CL2.Item1", text="2018 paper")
        self.cl.hlist.add("C.CL2.Item2", text="2019 paper")
        self.cl.hlist.add("C.CL3", text="Programming language")
        self.cl.hlist.add("C.CL3.Item1", text="Python")
        self.cl.hlist.add("C.CL3.Item2", text="java")

        self.cl.setstatus("C", "off")
        self.cl.setstatus("C.CL1", "off")
        self.cl.setstatus("C.CL1.Item1", "off")
        self.cl.setstatus("C.CL1.Item2", "off")

        self.cl.setstatus("C.CL2", "on")
        self.cl.setstatus("C.CL2.Item1")
        self.cl.setstatus("C.CL2.Item2")

        self.cl.setstatus("C.CL3", "off")
        self.cl.setstatus("C.CL3.Item1", "off")
        self.cl.setstatus("C.CL3.Item2", "off")
        self.cl.autosetmode()

def main():
    root = Tix.Tk()
    view = View(root)
    root.update()
    root.mainloop()

if __name__ == '__main__':
    main()

参考图像,将复选框添加到树视图,如果我 select GeeksforGeeks 它应该 select 所有子元素,如果我 select 编程语言它应该 select python 和 java。同样适用于 unselect also

源代码- How to create a tree view with checkboxes in Python

似乎没有内置方法可以做到这一点。您可能需要遍历子项来设置复选框

示例代码有两种方法 select_child 用于选择第一级子级和 select_children 用于选择所有子级。

import tkinter.tix as Tix

class View(object):
    def __init__(self, root):
        self.root = root
        self.makeCheckList()

    def makeCheckList(self):
        #self.cl = Tix.CheckList(self.root, command=self.select_child, browsecmd=self.select_child)
        self.cl = Tix.CheckList(self.root, command=self.select_children, browsecmd=self.select_children)
        self.cl.pack(expand=1, fill='both')
        self.cl.hlist.add("C", text="Top")
        #self.cl.hlist.bind("<Button-1>", self.select)

        self.cl.hlist.add("C.CL1", text="Computer Science")
        self.cl.hlist.add("C.CL1.Item1", text="Algorithm")
        self.cl.hlist.add("C.CL1.Item2", text="Data Structures")
        self.cl.hlist.add("C.CL1.Item2.Item3", text="python")

        self.cl.hlist.add("C.CL2", text="Gate Paper")
        self.cl.hlist.add("C.CL2.Item1", text="2018 paper")
        self.cl.hlist.add("C.CL2.Item2", text="2019 paper")

        self.cl.hlist.add("C.CL3", text="Programming language")
        self.cl.hlist.add("C.CL3.Item1", text="Python")
        self.cl.hlist.add("C.CL3.Item2", text="java")

        self.cl.setstatus("C", "off")
        self.cl.setstatus("C.CL1", "off")
        self.cl.setstatus("C.CL1.Item1", "off")
        self.cl.setstatus("C.CL1.Item2", "off")
        self.cl.setstatus("C.CL1.Item2.Item3", "off")

        self.cl.setstatus("C.CL2", "off")
        self.cl.setstatus("C.CL2.Item1", "off")
        self.cl.setstatus("C.CL2.Item2", "off")

        self.cl.setstatus("C.CL3", "off")
        self.cl.setstatus("C.CL3.Item1", "off")
        self.cl.setstatus("C.CL3.Item2", "off")
        self.cl.autosetmode()

    def select_children(self, item):  # selects all the children

        children = self.cl.hlist.info_children(item)
        status = self.cl.getstatus(item)
        
        for child in children:
            self.cl.setstatus(child, status)
            grand_child = self.cl.hlist.info_children(child)
            while grand_child:
                for x in grand_child:
                    self.cl.setstatus(x, status)
                    grand_child = self.cl.hlist.info_children(x)
            

    def select_child(self, item):  # selects only the first level children
        children = self.cl.hlist.info_children(item)
        status = self.cl.getstatus(item)

        for child in children:
            self.cl.setstatus(child, status)
        
def main():
    root = Tix.Tk()
    view = View(root)
    root.update()
    root.mainloop()

if __name__ == '__main__':
    main()

参考这些文档1 2