2011-02-08 72 views
1

我有一個應用程序,我正在使用gtk在python中編寫,我希望它自動關閉括號'並將光標放在它們之間問題是我隨機得到以下錯誤並且讓程序崩潰:gtk文本問題

./mbc.py:266: GtkWarning: Invalid text buffer iterator: either the iterator is 
uninitialized, or the characters/pixbufs/widgets in the buffer have been modified since 
the iterator was created. 
    You must use marks, character numbers, or line numbers to preserve a position across 
buffer modifications. 
    You can apply tags and insert marks without invalidating your iterators, 
    but any mutation that affects 'indexable' buffer contents (contents that can be 
referred to by character offset) 
    will invalidate all outstanding iterators 
     buff.place_cursor(buff.get_iter_at_line_offset(itter.get_line(),Iter.get_offset()-1)) 
    ./mbc.py:266: GtkWarning: gtktextbtree.c:4094: char offset off the end of the line 
     buff.place_cursor(buff.get_iter_at_line_offset(itter.get_line(),Iter.get_offset()-1)) 

    Gtk-ERROR **: Char offset 568 is off the end of the line 
    aborting... 
    Aborted 

在那個區域周圍的代碼是這樣的:

def insert_text(self, buff, itter, text, length): 
    if text == '(': 
     buff.insert_at_cursor('()') 
     mark = buff.get_mark('insert') 
     Iter = buff.get_iter_at_mark(mark) 
     buff.place_cursor(buff.get_iter_at_line_offset(itter.get_line(),Iter.get_offset()-1)) 

誰能告訴我如何解決這個問題?我找不到任何其他方法將光標置於圓括號'

回答

1

之間的那個特定位置。insert_at_cursor調用使傳遞到您的函數的迭代器無效。當您在最後一行中引用該迭代器時,GTK +會顯示警告。此行爲在GTK+ Text Widget Overview中解釋。

修復這是一個問題不能重新使用迭代器,例如:

buff.insert_at_cursor(')') # This invalidates existing iterators. 
mark = buff.get_mark('insert') 
iter = buff.get_iter_at_mark(mark) # New iterator 
iter.backward_cursor_positions(1) 
buff.place_cursor(iter) 

(聲明:我沒有用過的GTK +文本構件在很長一段時間有可能更容易  /更優雅的方式來做同樣的事情,但這個人做這項工作。)