python - PyGtk - 在特定行的 TreeView 中设置复选框不可见

标签 python tree treeview gtk pygtk

我想让节点“parent 1”和“parent 1 的 child 1”中的复选框不可见。你知道如何解决这个问题吗? 我仍在努力修改特定行而不是整列的单元格渲染器。

enter image description here

这是代码:

#!/usr/bin/env python

# example basictreeview.py

import pygtk
pygtk.require('2.0')
import gtk

class BasicTreeViewExample:
    # close the window and quit
    def delete_event(self, widget, event, data=None):
        gtk.main_quit()
        return False

    def __init__(self):
        # Create a new window
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)

        self.window.set_title("Basic TreeView Example")

        self.window.set_size_request(200, 200)

        self.window.connect("delete_event", self.delete_event)

        # create a TreeStore with one string column to use as the model
        self.treestore = gtk.TreeStore(str)

        # we'll add some data now - 4 rows with 3 child rows each
        for parent in range(4):
            piter = self.treestore.append(None, ['parent %i' % parent])
            for child in range(3):
                self.treestore.append(piter, ['child %i of parent %i' %
                                              (child, parent)])

        # create the TreeView using treestore
        self.treeview = gtk.TreeView(self.treestore)

        # create the TreeViewColumn to display the data
        self.tvcolumn = gtk.TreeViewColumn('Column 0')

        # add tvcolumn to treeview
        self.treeview.append_column(self.tvcolumn)

        # create a CellRendererText to render the data
        self.cell = gtk.CellRendererText()
        self.cell1 = gtk.CellRendererToggle()

        # add the cell to the tvcolumn and allow it to expand
        self.tvcolumn.pack_start(self.cell, True)
        self.tvcolumn.pack_start(self.cell1, True)
        # set the cell "text" attribute to column 0 - retrieve text
        # from that column in treestore
        self.tvcolumn.add_attribute(self.cell, 'text', 0)

        # make it searchable
        self.treeview.set_search_column(0)

        # Allow sorting on the column
        self.tvcolumn.set_sort_column_id(0)

        # Allow drag and drop reordering of rows
        self.treeview.set_reorderable(True)

        swH = gtk.ScrolledWindow()
        swH.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        swH.add(self.treeview)
        self.window.add(swH)

        self.window.show_all()

def main():
    gtk.main()

if __name__ == "__main__":
    tvexample = BasicTreeViewExample()
    main()

我希望你能帮助我。

最佳答案

为可见添加一个属性并将 bool 放入您的树存储中,添加行时只需在添加子行时将其设置为 false。

gtk.TreeStore(str, bool)

self.tvcolumn.add_attribute(self.cell1, 'visible', 1)

self.treestore.append(None, ['parent %i' % parent, True])

self.treestore.append(piter, ['child %i of parent %i' %
                                          (child, parent), False])

这应该是您需要更改的大致内容。

请注意我不太了解 python 的语法(我使用 GTK 的 C# 绑定(bind))

关于python - PyGtk - 在特定行的 TreeView 中设置复选框不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32072487/

相关文章:

javascript - D3 树动态更新节点颜色

带有 PrimeFaces 的 Java Server Faces 树

.net - 更好的 Windows 窗体 TreeView - .NET 3.5

c# - 树形 View 和回发

python - 如何在 Python 中编写像 Jasmine 这样的嵌套测试用例

Python递归通过对象和子对象,打印子深度数

python - 在 Python 的一行中随机选择一个单词?

c++ - 类似文件夹树浏览器的 Windows 资源管理器

python - Python 中的函数给出错误消息

python - 如何使用 subprocess.check_call 一次性运行多个命令