2

gtk を使用して python で作成しているアプリがあり、括弧を自動的に閉じてカーソルをそれらの間に配置する必要があります。問題は、次のエラーがランダムに発生し、プログラムがクラッシュすることです。

    ./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))

このエラーを修正する方法を教えてもらえますか? かっこの間の特定の場所にカーソルを配置する他の fmethods が見つかりません。

4

1 に答える 1

2

このinsert_at_cursor呼び出しは、関数に渡された反復子を無効にします。最後の行でその反復子を参照すると、GTK+ は警告を表示します。この動作については、GTK+ テキスト ウィジェットの概要で説明されています。

これを修正するには、イテレータを再利用しないことです。たとえば、次のようになります。

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+ テキスト ウィジェットを使用していません。おそらく、同じことを行うためのより簡単でエレガントな方法がありますが、これでうまくいきます。)

于 2011-02-08T06:09:49.573 に答える