How to make column editable for particular child i

2019-08-13 15:27发布

问题:

In below picture you can see the 3 parent child window. actually I made one code which will show all child according to parent. that is showing correctly but i want username & password column editable for database parent only(like as 3rd window). other should not be editable.

In my case all child of parent is editable.

here is my code

#!/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()

Anybody help me for this problem.

Thanks...

回答1:

The answer here is similar to the answer to your previous question: you need to extend your tree store with an additional column that specifies the attribute you care about — in this case, editability of username/password cells for a particular row, and connect that store column with the tree view columns that display and edit the username and password.

The provided code simply sets the editable property to true, which means the property is editable everywhere.

There are tutorials that explain the GTK tree view in some detail. Here is a version of the code from the question, modified to match your requirements as I understood them.

#!/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()