(Python Gtk) Update ListBox Content

Asked by At

I am trying to dynamically add an element to a ListBox. It works without a function but not with.

Works

rootbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)

button = Gtk.Button(label="ok")
listbox = Gtk.ListBox()

el = Gtk.ListBoxRow()
label = Gtk.Label('Element 0')
el.add(label)
listbox.add(el)

button.connect("clicked", addList)

rootbox.pack_start(button, False, True, 0)
rootbox.pack_start(listbox, True, True, 0)

window.add(rootbox)

Does not work (it prints 'ok' but doesn't add the element)

# -------------------
def addList(b):
    print('ok')
    global listbox
    el = Gtk.ListBoxRow()
    label = Gtk.Label('Element 0')
    listbox.add(el)
# -------------------

rootbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)

button = Gtk.Button(label="ok")
listbox = Gtk.ListBox()

button.connect("clicked", addList)

rootbox.pack_start(button, False, True, 0)
rootbox.pack_start(listbox, True, True, 0)

window.add(rootbox)

# -------------------------------------------

Thanks in advance

1 Answers

0
Narek On

window.show_all() needed at the end of the addList function ! Hope it helps