Why set_model method in PyGtk duplicate the value of the first column at the third column?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Why set_model method in PyGtk duplicate the value of the first column at the third column?

hudsonsferreira

I'm using PyGtk to display some string information in a treeview. Here goes my code:

def create_table(self):
    self.mainbox = gtk.ScrolledWindow()
    self.mainbox.set_policy( gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
    self.window.add( self.mainbox)

    model = gtk.ListStore( str, str, str)
    self.treeview = gtk.TreeView(model=None)

    col = gtk.TreeViewColumn( "Element")
    self.treeview.append_column( col)
    cell = gtk.CellRendererText()
    col.pack_start( cell, expand=False)
    col.set_attributes( cell, text=0)

    col = gtk.TreeViewColumn( "Test")
    self.treeview.append_column( col)
    cell = gtk.CellRendererSpin() 
    col.pack_start( cell, expand=False)
    col.set_attributes( cell, text=1)

    col = gtk.TreeViewColumn( "Command")
    self.treeview.append_column( col)
    cell = gtk.CellRendererSpin()
    col.pack_start( cell, expand=False)
    col.set_attributes( cell, text=0)

    cell = gtk.CellRendererCombo()
    self.mainbox.add( self.treeview)
    self.mainbox.set_size_request( 500, 260)
    self.mainbox.show()
    self.vbox.pack_start(self.mainbox, expand=False, fill=True, padding=0)

Then, I created a method to fill the treeview after an event button. The call:

 def populate_treeview_button(self):
    button = gtk.Button(label='Populate Table')
    button.connect("clicked", self.create_model)
    self.vbox.pack_start(button, expand=False, fill=True, padding=0)

And the method (I receive a list of dicts at table_information attribute where the key is an element(string) and the value a list with 2 strings):

def create_model(self, beats_me_param):
    model = gtk.ListStore( str, str, str)

    elements = []
    tests = []
    commands = []

    table_information = self.get_organized_table()

    for i in table_information:
        for dicts in i:
            for element in dicts.keys():
                elements.append(element)

    for i in table_information:
        for dicts in i:
            for value in dicts.values():
                tests.append(value[0])
                commands.append(value[1])


    for i in range(len(elements)):
        model.append([elements[i], tests[i], commands[i]])            

    self.treeview.set_model(model)

When I saw the result at my treeview, I got at third column the same value of the first column and of course they are different. Image bellow: table

I changed the order of elements in the "append" moment and same occurs, the value changes but the changed value is repeated at the third column. What's going wrong?

ps: The question was asked at the stackoverflow. Link.

Regards, 
--
Hudson Silva Ferreira

_______________________________________________
pygtk mailing list   [hidden email]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/
Reply | Threaded
Open this post in threaded view
|

Re: Why set_model method in PyGtk duplicate the value of the first column at the third column?

Neil Muller-10
On 12 December 2013 22:06, Hudson Ferreira <[hidden email]> wrote:
>     col.pack_start( cell, expand=False)
>     col.set_attributes( cell, text=0)

You look up the values for the first colum in your view in the first
column of the model (column 0) - that's fine.

>     col.pack_start( cell, expand=False)
>     col.set_attributes( cell, text=1)

You look up the values for the seond column in your view in the second
column of the model (column 1).

>     col.set_attributes( cell, text=0)

But you use the first column of your model to display the third column
of the model - For your comments, this is not what you want. This
should almost certainly be col.set_attributes( cell, text=2)

--
Neil Muller
[hidden email]

I've got a gmail account. Why haven't I become cool?
_______________________________________________
pygtk mailing list   [hidden email]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/