如何在 Python 中创建带有复选框的树视图 [英] How to create a tree view with checkboxes in Python

查看:34
本文介绍了如何在 Python 中创建带有复选框的树视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 Tkinter 和 Tix 编写一个小程序.我现在需要一个带有复选框(复选框)的树视图,以便我可以从树视图中选择项目.是否有捷径可寻?我一直在看 ttk.Treeview () 并且它看起来很容易获得树视图但是有没有办法在视图中插入一个复选按钮?

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.

我不仅限于 ttk.什么都行;只要我有一个例子或好的文档,我就可以让它工作

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

推荐答案

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()

这篇关于如何在 Python 中创建带有复选框的树视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆