我想创建一个需要不断更新 ListBox
的 Gtk 程序,即添加和删除行。我正在尝试这种方法。它可能更干净,但在这里它足以解释问题。
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from time import sleep
from threading import Thread
def add_widgets(listbox):
# add 5 rows
for _ in range(5):
for _ in range(5):
add_row(listbox)
window.show_all() # make the changes visible
sleep(1) # to make the change stay
# remove all the rows
for row in listbox.get_children():
listbox.remove(row)
window.show_all()
sleep(1)
# all the addition and removal done
print('finished')
def add_row(listbox):
listboxrow = Gtk.ListBoxRow()
label = Gtk.Label()
label.set_text("Hey There")
listboxrow.add(label)
listbox.add(listboxrow)
window = Gtk.Window()
window.connect('destroy', Gtk.main_quit)
listbox = Gtk.ListBox()
window.add(listbox)
window.show_all()
# Thread to add and remove rows
update_thread = Thread(target=add_widgets, args=(listbox, ))
update_thread.start()
Gtk.main()
这段代码在 50% 的时间内运行良好。其余的,它给了我 3 种类型的错误,它们都是随机的。SegFault
在 add_widgets
中对父循环进行 2 或 3 次迭代后**
Gtk:ERROR:../gtk/gtk/gtkcssnode.c:319:lookup_in_global_parent_cache: assertion failed: (node->cache == NULL)
Bail out! Gtk:ERROR:../gtk/gtk/gtkcssnode.c:319:lookup_in_global_parent_cache: assertion failed: (node->cache == NULL)
Aborted
我已经尝试在适当的地方添加更多的 sleep 语句,因为最初,我想,这可能是因为在更新小部件时窗口没有准备好。
我想知道它为什么会发生,我该如何解决。
编辑 :这可能与 UI 和线程同步有关,因为在与窗口交互时它(崩溃)发生得更频繁。例如,当您拖动它时,它更有可能崩溃。
最佳答案
您不得从其他线程调用 GTK+ 代码。所有交互都必须在主线程中完成。这记录在 https://developer.gnome.org/gdk3/stable/gdk3-Threads.html 中.
如果您想对您的小部件进行一些“后台”更新,您可以使用诸如 g_idle_add()
之类的函数来模拟它。 ( https://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html#g-idle-add ), g_timeout_add()
( https://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html#g-timeout-add ) 和其他相关功能。
关于python - Gtk 在添加和删除列表框行时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67146064/