如何在pygtk中为特定子项创建列

时间:2014-01-13 09:58:39

标签: python python-2.7 treeview gtk pygtk

在下图中,您可以看到3父子窗口。实际上我做了一个代码,根据父母显示所有孩子。这显示正确,但我想要用户名和&密码列仅可用于数据库父级(如第3个窗口)。其他不应该是可编辑的。

在我的情况下,父母的所有孩子都是可编辑的。 enter image description here

这是我的代码

#!/usr/bin/env python

import pygtk
pygtk.require('2.0')
import gtk, gobject
import os
import cairo, gio, pango, pangocairo, atk

class BasicTreeViewExample:

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

    def col1_toggled_cb( self, cell, path, model ):
        model[path][1] = not model[path][1]

    def on_cell_edited(self, cell, path_string, new_text, model):
        print "hi"
        #cell.set_property('editable', False)
        iter = model.get_iter_from_string(new_text)
        path = model.get_path(iter)[0]
        column = cell.get_data("username")

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

        self.window.connect("delete_event", self.delete_event)
        self.treestore = gtk.TreeStore(gobject.TYPE_STRING, gobject.TYPE_BOOLEAN,gobject.TYPE_BOOLEAN, gobject.TYPE_STRING, gobject.TYPE_STRING)
        for parent in range(2):
            piter = self.treestore.append(None, ['Computer %i' % parent, False,False, '', ''])
            pit = self.treestore.append(piter, ['Drive', False,False, '', ''])
            pith = self.treestore.append(piter, ['Database', False,False, '', ''])
            for child in range(2):
                self.treestore.append(pit, ['child', True,True, '', ''])
            for child in range(2):
                self.treestore.append(pith, ['child', True,True, 'user', 'psw'])
        self.treeview = gtk.TreeView(self.treestore)

        self.cell = gtk.CellRendererText()
        self.tvcolumn = gtk.TreeViewColumn("Computer Name", self.cell, text=0)
        self.cell.set_property( 'editable', False )

        self.cell0 = gtk.CellRendererToggle()
        self.tvcolumn0 = gtk.TreeViewColumn("Select Drive", self.cell0 , active=1, visible=2)
        self.cell0.set_property('activatable', True)
        self.cell0.connect( 'toggled', self.col1_toggled_cb, self.treestore )

        self.cell1 = gtk.CellRendererText()
        self.tvcolumn1 = gtk.TreeViewColumn("username", self.cell1, text=3, visible=2)
        self.cell1.set_property( 'editable', True )
        self.cell1.connect("editing-started", self.on_cell_edited, self.treestore)

        self.cell2 = gtk.CellRendererText()
        self.tvcolumn2 = gtk.TreeViewColumn("password", self.cell2, text=4, visible=2)
        self.cell2.set_property( 'editable', True )
        self.treeview.append_column(self.tvcolumn)
        self.treeview.append_column(self.tvcolumn0)
        self.treeview.append_column(self.tvcolumn1)
        self.treeview.append_column(self.tvcolumn2)
        self.treeview.set_reorderable(True)
        self.window.add(self.treeview)
        self.window.show_all()

def main():
    gtk.main()

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

任何人都可以帮我解决这个问题。

...谢谢

1 个答案:

答案 0 :(得分:0)

这里的答案类似于上一个问题的答案:您需要使用指定您关注的属性的附加列扩展树存储 - 在这种情况下,特定行的用户名/密码单元格的可编辑性,并将该存储列与显示和编辑用户名和密码的树视图列连接。

提供的代码只是将editable属性设置为true,这意味着该属性在任何地方都可以编辑。

tutorials详细解释了GTK树视图。以下是问题代码的一个版本,根据我的理解进行了修改以符合您的要求。

#!/usr/bin/env python

import pygtk
pygtk.require('2.0')
import gtk, gobject
import os
import cairo, gio, pango, pangocairo, atk

class BasicTreeViewExample:

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

    def col1_toggled_cb( self, cell, path, model ):
        model[path][1] = not model[path][1]

    def on_cell_edited(self, cell, path_string, new_text, model):
        print "hi"
        #cell.set_property('editable', False)
        iter = model.get_iter_from_string(new_text)
        path = model.get_path(iter)[0]
        column = cell.get_data("username")

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

        self.window.connect("delete_event", self.delete_event)
        self.treestore = gtk.TreeStore(gobject.TYPE_STRING, gobject.TYPE_BOOLEAN,gobject.TYPE_BOOLEAN, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_BOOLEAN)
        for parent in range(2):
            piter = self.treestore.append(None, ['Computer %i' % parent, False,False, '', '', False])
            pit = self.treestore.append(piter, ['Drive', False,False, '', '', False])
            pith = self.treestore.append(piter, ['Database', False,False, 'user', 'psw', True])
            for child in range(2):
                self.treestore.append(pit, ['child', True,True, '', '', False])
            for child in range(2):
                self.treestore.append(pith, ['child', True,True, '', '', False])
        self.treeview = gtk.TreeView(self.treestore)

        self.cell = gtk.CellRendererText()
        self.tvcolumn = gtk.TreeViewColumn("Computer Name", self.cell, text=0)
        self.cell.set_property( 'editable', False )

        self.cell0 = gtk.CellRendererToggle()
        self.tvcolumn0 = gtk.TreeViewColumn("Select Drive", self.cell0 , active=1, visible=2)
        self.cell0.set_property('activatable', True)
        self.cell0.connect( 'toggled', self.col1_toggled_cb, self.treestore )

        self.cell1 = gtk.CellRendererText()
        self.tvcolumn1 = gtk.TreeViewColumn("username", self.cell1, text=3, visible=5, editable=5)
        self.cell1.connect("editing-started", self.on_cell_edited, self.treestore)

        self.cell2 = gtk.CellRendererText()
        self.tvcolumn2 = gtk.TreeViewColumn("password", self.cell2, text=4, visible=5, editable=5)
        self.treeview.append_column(self.tvcolumn)
        self.treeview.append_column(self.tvcolumn0)
        self.treeview.append_column(self.tvcolumn1)
        self.treeview.append_column(self.tvcolumn2)
        self.treeview.set_reorderable(True)
        self.window.add(self.treeview)
        self.window.show_all()

def main():
    gtk.main()

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