5

I'm new to coding and this is probably a stupid question...

I'm setting up a simple Gtk.ListStore(str) but as soon as I append(['whatever']) I get a "AttributeError: 'ListStore' object has no attribute 'insert_with_valuesv'".

I'm on Fedora 34(beta), python 3.9, GTK4.

This is a minimum example where the error happens:

#!/usr/bin/python
import gi
gi.require_version('Gtk','4.0')
from gi.repository import Gtk

simple = Gtk.ListStore(str)
simple.append(['whatever'])
Traceback (most recent call last):
  File "/home/dedum/prova/provagtk2.py", line 7, in <module>
    simple.append(['whatever'])
  File "/usr/lib/python3.9/site-packages/gi/overrides/Gtk.py", line 1009, in append
    return self._do_insert(-1, row)
  File "/usr/lib/python3.9/site-packages/gi/overrides/Gtk.py", line 1001, in _do_insert
    treeiter = self.insert_with_valuesv(position, columns, row)
AttributeError: 'ListStore' object has no attribute 'insert_with_valuesv'

What am I doing wrong?

Dan D.
  • 73,243
  • 15
  • 104
  • 123
  • 1
    I found a simple workaround, though it does not answer the question. First call Gtk.ListStore.append() without argument, then Gtk.ListStore.set(with_the_right_arguments). – user1579054 Apr 12 '21 at 22:16

3 Answers3

0

In Gtk 4 you can use the insert_with_values() method:

list_store = Gtk.ListStore.new([GObject.TYPE_INT, GObject.TYPE_STRING])

values = [(1, 'Item 1'),(2, 'Item 2')]
for value in values:
    # insert_with_values(row, columns, values)
    list_store.insert_with_values(value[0], (0, 1), value)
  • row: Item position.
  • columns: Position of the columns. GObject.TYPE will validate the type, in this example the first column is an int and the second a string.
  • values: A tuple (or list) containing the values that will be displayed.

I'm also having a bit of difficulty migrating from Gtk 3 to Gtk4.

To make it a little easier I created a repository where I'm putting Gtk 4 code examples for python (PyGObject):

Renato Cruz
  • 121
  • 5
0

Your code is correct, the problem is an interaction bug between Gtk+ 4.1 or later and pygobject 3.42 or earlier. It was fixed in the following commit:

We'll need to wait for a new release of pygobject for now, so use insert_with_values() instead of append() for now.

hdante
  • 7,685
  • 3
  • 31
  • 36
-1

Had the same problem. I solved it by setting the gtk version to 3.0 gi.require_version('Gtk','4.0')

Dharman
  • 30,962
  • 25
  • 85
  • 135
nskrz
  • 1