Python3 GtkTextBuffer核心转储

2024-06-11 20:37:07 发布

您现在位置:Python中文网/ 问答频道 /正文

使用Python3和gi.repository.Gtk,我试图通过GtkTextBuffer显示GtkTextView内的多个文本行。你知道吗

基本上,我使用_addLine方法动态添加行,该方法以这种方式更新文本缓冲区(self._lines是数组,self._textBufferGtkTextBuffer):

def _addLine(self, text):
    if len(self._lines) == self._maxLines:
        self._lines = self._lines[1:]
    self._lines.append(text)
    content = '\n'.join(self._lines)
    print("TIC: %d" % len(content))
    self._textBuffer.set_text(content)
    print("TAC")

不幸的是,当随机值为i(小于或大于self._maxLines)时,我会在“TIC”和“TAC”之间随机得到一个核心转储,因此当我尝试设置缓冲区的内容时。你知道吗

此方法由线程调用,线程本身由构造函数调用(在初始化所有GUI元素之后):

def _startUpdateThread(self):
    thread = threading.Thread(target=lambda: self._updateLoop())
    thread.daemon = True
    thread.start()

def _updateLoop(self):
    i=0
    for l in listings.tail(self._logFile, follow=True, n=1000):
        i+=1
        print("i=%d, nLines=%d" % (i, len(self._lines)))
        self._addLine(l)

我正在使用一个Glade builder,结构如下:

GtkWindow
  - GtkVBox
      - GtkScrolledWindow
          - GtkTextView (linked to GtkTextBuffer)
  - GtkButton (to close the window)
  - GtkTextBuffer

我做错了什么?堆芯倾倒的原因是什么?你知道吗

非常感谢你的帮助。你知道吗


Tags: 方法text文本selflendefcontentthread
1条回答
网友
1楼 · 发布于 2024-06-11 20:37:07

当从线程而不是GTK主循环修改小部件时,应该使用GLib.idle_add()。你知道吗

在这种情况下:

GLib.idle_add(self._textBuffer.set_text, content)

相关问题 更多 >