Home > Software engineering >  Pygtk Liststore most recent call last after Liststore update
Pygtk Liststore most recent call last after Liststore update

Time:04-06

I created simple code in PyGtk that displays a list of names. After clicking the Update button, the list of names will be updated. Unfortunately, I can't get rid of the "most recent call last" error that appears after clicking the Update button. Can anyone advise me on the problem? This is probably a problem with a line of code under "# select and row".

EDIT: Script returns error message:

    Traceback (most recent call last):
  File "/home/radek/Desktop/stackowr1.py", line 18, in choiceRow
    line = model[treeiter][0]
  File "/usr/lib/python3/dist-packages/gi/overrides/Gtk.py", line 849, in __getitem__
    aiter = self._getiter(key)
  File "/usr/lib/python3/dist-packages/gi/overrides/Gtk.py", line 837, in _getiter
    aiter = self.get_iter(key)
  File "/usr/lib/python3/dist-packages/gi/overrides/Gtk.py", line 871, in get_iter
    path = self._coerce_path(path)
  File "/usr/lib/python3/dist-packages/gi/overrides/Gtk.py", line 846, in _coerce_path
    return TreePath(path)
  File "/usr/lib/python3/dist-packages/gi/overrides/Gtk.py", line 1210, in __new__
    path = ":".join(str(val) for val in path)
TypeError: 'NoneType' object is not iterable

My code:

```python
# -*- coding: utf-8 -*-
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

class MyWindow(Gtk.Window):

    def __init__(self):
        super(MyWindow, self).__init__()
        self.set_border_width(3)
        self.set_default_size(800, 600)

        self.name1 = "John"
        self.name2 = "George"

        def choiceRow(selection):
            model, treeiter = selection.get_selected()
            line = model[treeiter][0]
            print(line)

        def update_name(self):
            self.name1 = "Jeane"
            self.name2 = "Margot"
            print(self.name1, self.name2)
            win.liststore.clear()
            win.liststore.append([self.name1])
            win.liststore.append([self.name2])

        button = Gtk.Button(label = "Update")
        button.connect("clicked", update_name)

        self.layout = Gtk.Layout()

        self.tree = Gtk.TreeView()
        self.liststore = Gtk.ListStore(str)
        self.tree.set_model(self.liststore)
        self.liststore.append([self.name1])
        self.liststore.append([self.name2])
        render = Gtk.CellRendererText()
        self.column = Gtk.TreeViewColumn("ID", render, text=0)
        self.tree.append_column(self.column)

        # select a row
        selectetRow = self.tree.get_selection()
        selectetRow.connect("changed", choiceRow)

        self.layout.put(self.tree, 0,0)
        self.layout.put(button, 0,100)

        self.add(self.layout)

win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

CodePudding user response:

Gtk.TreeSelection.get_selected returns None when nothing is selected, after the update call there's nothing selected so you get None for treeiter and you try to access model[None][0] which obviously must fail. You need to check whether the returned iter is valid before trying to use it so just change your choiceRow function to

def choiceRow(selection):
     model, treeiter = selection.get_selected()
     if treeiter:
          line = model[treeiter][0]
          print(line)
  • Related