简体   繁体   English

(Python Gtk)更新ListBox内容

[英](Python Gtk) Update ListBox Content

I am trying to dynamically add an element to a ListBox. 我试图动态添加元素到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) 不起作用(打印'ok'但不添加元素)

# -------------------
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 提前致谢

window.show_all() needed at the end of the addList function ! window.show_all()需要在addList函数的末尾! Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM