2010-11-23 8 views
4

TextView와 TextBuffer가 연관되어 있습니다.pyGTK의 TextView/TextBuffer에 굵은 숯불 문자 삽입하기

사용자가 Ctrl + b를 누르면 사용자가 Ctrl + b를 다시 누를 때까지 텍스트가 굵은 체로 시작되기를 원합니다.

I가 작동하지 않은 내 자신의 방법을 시도 한 후, 나는 메일 링리스트에서 해당 게시물을 발견 :

나 같은 http://www.daa.com.au/pipermail/pygtk/2009-April/016951.html

같은 문제 및 솔루션 누군가가 준입니다

응용 프로그램에서 TextBuffer의 태그를 관리하는 데 필요한 부기를 처리해야합니다. 텍스트가 커서에 삽입되면 앱 은 텍스트가 삽입되었음을 나타내는 신호를 잡아야하고 삽입 된 텍스트에 필수 태그를 적용해야합니다. 나는 텍스트가 이미 삽입되었는지 확인하기 위해 connect_after() 을 사용하여 TextBuffer "insert-text"신호를 잡아서 콜백의 텍스트에 태그를 적용하여 을 수행 할 수 있다고 생각합니다.

그래서 이것을 시도했습니다. 이것은 내 textbuffer.py입니다

import gtk 
import pango 

class TextBuffer(gtk.TextBuffer): 
def __init__(self): 
    gtk.TextBuffer.__init__(self) 
    self.connect_after('insert-text', self.text_inserted) 
    # A list to hold our active tags 
    self.tags_on = [] 
    # Our Bold tag. 
    self.tag_bold = self.create_tag("bold", weight=pango.WEIGHT_BOLD) 

def get_iter_position(self): 
    return self.get_iter_at_mark(self.get_insert()) 

def make_bold(self, text): 
    ''' add "bold" to our active tags list ''' 
    self.tags_on.append('bold') 

def text_inserted(self, buffer, iter, text, length): 
    # A text was inserted in the buffer. If there are ny tags in self.tags_on, apply them 
    if self.tags_on: 
    print self.get_iter_position() 

    # This sets the iter back N characters 
    iter.backward_chars(length) 

    # And this applies tag from iter to end of buffer 
    self.apply_tag_by_name('bold', self.get_iter_position(), self.get_end_iter()) 

    print self.get_iter_position() 

누군가가 Ctrl + b를 누를 때마다 make_bold() 메소드가 기본 스크립트에서 호출됩니다.

이론적으로 이것은 메일 링 도움이 말한 것을 정확하게 수행하고 있습니다. 그러나 작동하지 않습니다. 내가 입력 할 때 텍스트가 굵게 표시되지 않습니다. 왼쪽 화살표를 누르고 커서를 뒤로 이동시킨 다음 문자를 입력하면 커서 오른쪽의 문자가 굵게 표시됩니다.

이 작업을 어떻게 수행 할 수 있습니까?

또한 '누군가이 태그에'textbuffer '를 추가 할 수 있습니까? 나는 새로운 태그를 만들 수 없습니다 나는!,이 태그는 TextBuffer.text_insertediter.backward_chars에 전화를하지만 당신은 그 ITER를 사용하지 않을 샘플 코드에서 '텍스트 뷰'

답변

5

보다 더 정확 같은 느낌 그래서 샘플 스크립트를 만들 원하는 행동을 표시하고 명확히하십시오 :

import gtk 
import pango 

class TextBuffer(gtk.TextBuffer): 
    def __init__(self): 
     gtk.TextBuffer.__init__(self) 
     self.connect_after('insert-text', self.text_inserted) 
     # A list to hold our active tags 
     self.tags_on = [] 
     # Our Bold tag. 
     self.tag_bold = self.create_tag("bold", weight=pango.WEIGHT_BOLD) 

    def get_iter_position(self): 
     return self.get_iter_at_mark(self.get_insert()) 

    def make_bold(self): 
     ''' add "bold" to our active tags list ''' 
     if 'bold' in self.tags_on: 
      del self.tags_on[self.tags_on.index('bold')] 
     else: 
      self.tags_on.append('bold') 

    def text_inserted(self, buffer, iter, text, length): 
     # A text was inserted in the buffer. If there are ny tags in self.tags_on, apply them 
     if self.tags_on: 
      # This sets the iter back N characters 
      iter.backward_chars(length) 
      # And this applies tag from iter to end of buffer 
      self.apply_tag_by_name('bold', self.get_iter_position(), iter) 



def main(): 
    window = gtk.Window() 
    window.connect('destroy', lambda _: gtk.main_quit()) 
    window.resize(300, 300) 
    tb = TextBuffer() 
    tv = gtk.TextView(buffer=tb) 

    accel = gtk.AccelGroup() 
    accel.connect_group(gtk.keysyms.b, 
         gtk.gdk.CONTROL_MASK,gtk.ACCEL_LOCKED, 
         lambda a,b,c,d: tb.make_bold()) 
    window.add_accel_group(accel) 
    window.add(tv) 
    window.show_all() 
    gtk.main() 

if __name__ == '__main__': 
    main() 
+0

생명의 은인! 해결책에 대해 많은 감사드립니다. – sqram