2015-05-12 65 views
0

使用Python3与gi.repository.Gtk,我试图通过GtkTextBufferGtkTextView内显示多个文本行。Python3 gi:GtkTextBuffer核心转储

基本上,我动态地添加使用_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的助洗剂结构如下:

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

我做错了什么?核心转储的原因是什么?

非常感谢您的帮助。

回答

3

当您从线程修改窗​​口部件而不是GTK主循环时,应该使用GLib.idle_add()

在这种情况下:

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

非常感谢@ elya5我必须设置里面'updateLoop'取代'内addLine',使其工作。 –