2015-05-12 66 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',使其工作。 –