2011-02-08 5 views
1

저는 파이썬에서 gtk를 사용하여 작성 중이며 자동으로 괄호를 닫고 그 사이에 커서를 놓기를 원합니다. 문제는 다음 오류를 무작위로 얻고 있습니다. 프로그램이 다운 :gtk 텍스트 문제 itter

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

사람이 어떻게이 오류를 수정하는 방법을 말해 줄래 :

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

그 지역 주변의 코드는 무엇입니까? 괄호 사이에 커서를 위치시키는 다른 fmethod를 찾을 수 없습니다. '

답변

1

insert_at_cursor 호출은 함수에 전달 된 반복자를 무효화합니다. 마지막 라인의 iterator를 다시 참조하면 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 + 텍스트 위젯을 사용하지 않은 쉬운  /똑같은 일을하는 더 우아한 방법이지만,이 일은 잘합니다.)