0

I have a textfile with the current data:

Firefox 2002 C++
Eclipse 2004 Java 
Pitivi 2004 Python
Netbeans 1996 Java
Chrome 2008 C++
Filezilla 2001 C++
Bazaar 2005 Python
Git 2005 C
Linux Kernel 1991 C
GCC 1987 C
Frostwire 2004 Java

I want to read it from my python program and add it to a ListStore to have something like this

https://i.stack.imgur.com/fqnvM.png

This is my code:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

filename = 'data.txt'
with open(filename) as f:
    data = f.readlines()

class TreeViewFilterWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Treeview Filter Demo")
        self.grid = Gtk.Grid()
        self.grid.set_column_homogeneous(True)
        self.grid.set_row_homogeneous(True)
        self.add(self.grid)
        self.software_liststore = Gtk.ListStore(str, str, str)
        self.software_liststore.append(data[0].split())
        self.software_liststore.append(data[10].split())
        i=0
        while (i<(len(data))):
            print(data[i].split())
            self.software_liststore.append(data[i].split())
            i=i+1

        self.treeview = Gtk.TreeView(model=self.software_liststore)
        for i, column_title in enumerate(["Software", "Release Year", "Programming Language"]):
            renderer = Gtk.CellRendererText()
            column = Gtk.TreeViewColumn(column_title, renderer, text=i)
            self.treeview.append_column(column)

        self.scrollable_treelist = Gtk.ScrolledWindow()
        self.scrollable_treelist.set_vexpand(True)
        self.grid.attach(self.scrollable_treelist, 0, 0, 8, 10)
        self.scrollable_treelist.add(self.treeview)
        self.show_all()

win = TreeViewFilterWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

The problem is that there is no problem doing this:

self.software_liststore.append(data[0].split())
self.software_liststore.append(data[10].split())

But when I try to use the while loop to insert data, it says:

Traceback (most recent call last):
    File "tut20.py", line 83, in <module>
    win = TreeViewFilterWindow()
File "tut20.py", line 68, in __init__
self.software_liststore.append(data[i].split())
File "/usr/lib/python2.7/dist-packages/gi/overrides/Gtk.py", line 956, in append
return self._do_insert(-1, row)
File "/usr/lib/python2.7/dist-packages/gi/overrides/Gtk.py", line 947, in _do_insert
row, columns = self._convert_row(row)
File "/usr/lib/python2.7/dist-packages/gi/overrides/Gtk.py", line 849, in _convert_row
raise ValueError('row sequence has the incorrect number of elements')

ValueError: row sequence has the incorrect number of elements

What am I doing wrong?

gmv92
  • 105
  • 1
  • 2
  • 9

3 Answers3

0

Ok, line 9 in data.txt is:

Linux Kernel 1991 C

which contains 4 elements. Try:

Linux_Kernel 1991 C

or simply:

Linux 1991 C
theGtknerd
  • 3,647
  • 1
  • 13
  • 34
0

This may because the number of elements returned by data[0].split() is greater than the number of columns. In other words, I think you give 4 or more elements to a row while it can only accept 3 elements. Please debug step by step and find out what's wrong with your program. Hope this will help.

haolee
  • 892
  • 9
  • 19
0

This sounds like a case for regular expressions.

Use Python's re regex module.

import re
self.software_liststore = Gtk.ListStore(str, str, str)
p = r"(?P<software>.+?)\s+(?P<release_year>\d{4})\s+(?P<programming_language>.+?)\r?\n"
for line in data:
    match = re.match(p, line)
    if  match:
        software = match['software']
        release_year = match['release_year']
        programming_language = match['programming_language']

        self.software_liststore.append([software, release_year, programming_language])

Also, note that software_liststore.append() apparently takes only a list of cells, not even a tuple.

shivdhar
  • 1
  • 1
  • 1