Hi,
I'm writing a set of examples for basic use of TreeView widget for
those who only need to manage lists (and not trees). Here in the
following there is an example wich shows a cryptic behaviour: a
list of items is shown (names, surnames) and when you click the button
name and surname should swap; the strange thing is about the update of
the view that is executed in a further moment, only when you force it
(window resize, mouse pointer over the list items, click in the list, etc.).

Why the update is delayed? How can I solve the problem in the Right
Way (TM)? (there are some dirty hacks, but they are not good to
show in an example)

Thanks in advance,

                                                        Emanuele

-----
example2.2.py
-----

import gtk

class Person(object):
    """A simple class"""
    def __init__(self, name="unknown", surname="unknown"):
        self.name=name
        self.surname=surname

def getname(treeview, cell, model, row):
    """Get name from the selected item of the list"""
    name=model.get_value(row,0).name
    cell.set_property("text", name)
    return

def getsurname(treeview, cell, model, row):
    """Get surnname from the selected item of the list"""
    surname=model.get_value(row,0).surname
    cell.set_property("text", surname)
    return

def action(*args):
    """Action to perform when an item of the list is selected"""
    model, row = view.get_selection().get_selected()
    print model.get_value(row,0).name, model.get_value(row,0).surname

def change(button, people):
    """Swap name and surname of all people"""
    for i in people:
        tmp=i.name
        i.name=i.surname
        i.surname=tmp

if __name__=='__main__':    

    # some data to fill the listview
    
people=[Person("Emanuele","Olivetti"),Person("Laura","Gatti"),Person("Richard","Stallman")]

    # define a model with with only 1 column (object)
    model = gtk.ListStore(object)

    # fill the model with the people
    for i in people:
        row=model.append()
        model.set_value(row, 0, i)

    # create a view based on the model
    view = gtk.TreeView(model)

    # create a text renderer to render the text
    renderer = gtk.CellRendererText()

    # create two columns based on name/surname data
    column0 = gtk.TreeViewColumn("Name", renderer)
    column0.set_cell_data_func(renderer, getname)
    column1 = gtk.TreeViewColumn("Surname", renderer)
    column1.set_cell_data_func(renderer, getsurname)

    # append column to the view
    view.append_column(column0)
    view.append_column(column1)

    # connect the signal "cursor-changed" to 'action' function
    view.connect('cursor-changed',action)

    # ListView ends here. Adding horizontal and vertical scroll bars
    
    sw=gtk.ScrolledWindow()
    sw.set_policy(gtk.POLICY_AUTOMATIC,gtk.POLICY_AUTOMATIC)
    sw.add(view)

    # Preparing a new button:
    b=gtk.Button()
    b.set_label("change!")
    b.connect("clicked",change,people)

    # putting everything in horizontal box to add a button
    hb=gtk.HBox()
    hb.add(sw)
    hb.add(b)

    # Now we need a window to insert everything...

    # create a window, insert the view, associate the 'mainquit' function
    # to the "destroy" signal and show all
    win = gtk.Window(gtk.WINDOW_TOPLEVEL)
    win.add(hb)
    win.connect("destroy", gtk.mainquit)
    win.show_all()

    # go...
    gtk.main()


----
_______________________________________________
pygtk mailing list   [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/

Reply via email to