How to create a tree view with checkboxes in Python

user600295 picture user600295 · Feb 24, 2011 · Viewed 17.7k times · Source

I've been using Tkinter and Tix to write a small program. I'm at a point where I need a tree view with checkboxes (checkbuttons) so I can select items from the tree view. Is there an easy way to do this? I've been looking at ttk.Treeview () and it looks easy to get the tree view but is there a way to insert a checkbutton to the view?

A simple code snippet would be really appreciated.

I'm not limited to ttk. Anything will do; as long as I have an example or good docs I can make it work

Answer

Brandon picture Brandon · Feb 24, 2011

enter image description here

import Tix

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

    def makeCheckList(self):
        self.cl = Tix.CheckList(self.root, browsecmd=self.selectItem)
        self.cl.pack()
        self.cl.hlist.add("CL1", text="checklist1")
        self.cl.hlist.add("CL1.Item1", text="subitem1")
        self.cl.hlist.add("CL2", text="checklist2")
        self.cl.hlist.add("CL2.Item1", text="subitem1")
        self.cl.setstatus("CL2", "on")
        self.cl.setstatus("CL2.Item1", "on")
        self.cl.setstatus("CL1", "off")
        self.cl.setstatus("CL1.Item1", "off")
        self.cl.autosetmode()

    def selectItem(self, item):
        print item, self.cl.getstatus(item)

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

if __name__ == '__main__':
    main()