halfdan
halfdan

Reputation: 34254

PyGTK TreeView showing blank rows from ListStore

I'm trying to show several rows from database in a TreeView but all I am getting are some dummy rows as you can see in the image below.

Application Screenshot

class SettingsDialog(gtk.Dialog):
    def __init__(self):
        gtk.Dialog.__init__(self, "Server Settings", self, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT)

        # Instantiate ServerManager
        self.server_manager = ServerManager()

        # Create TreeStore for Server list
        self.liststore = gtk.ListStore(str, str)
        self.treeview = gtk.TreeView(self.liststore)

        # Create TreeViewColumns to display data
        cell = gtk.CellRendererText()
        col = gtk.TreeViewColumn("Name")
        col.pack_start(cell, True)
        self.treeview.append_column(col)

        cell = gtk.CellRendererText()
        col = gtk.TreeViewColumn("URL")
        col.pack_start(cell, True)
        self.treeview.append_column(col)

        self.vbox.pack_start(self.treeview)

        self.resize(500,350)
        self.set_position(gtk.WIN_POS_CENTER)
        self.show_all()

        self.load_server_list()


    def load_server_list(self):
        self.liststore.clear()
        servers = self.server_manager.list()
        for name, url in servers.iteritems():
                self.liststore.append([name, url])
        self.show_all()

Data returned from self.server_manager.list() is valid an added to the list store perfectly. There seems to be something wrong with the CellRenderers but I wasn't able to find the error.

Upvotes: 4

Views: 1919

Answers (1)

XORcist
XORcist

Reputation: 4367

You have to set an attribute mapping on the column. For example, the cellrenderer's text attribute value will be displayed in the treeview cell. It is taken from the values on the data model (self.liststore). The column number on the model where the value is taken from is specified in the attribute mapping.

## Take value for *text* attribute of the cell renderer from the model's 3rd column
col = gtk.TreeViewColumn(title, cellrenderer, text=2)

Upvotes: 4

Related Questions