2012-01-20 8 views
1

모든 것이 설정되었으므로 Tkinter 텍스트 위젯에서 2 줄을 어떻게 바꾸는 지 알 수 없습니다. 그것은 비활성화되어 있고 다른 위젯들로 채워져 있기 때문에 나는 3 개의 버튼으로 장애/비 집중 텍스트 위젯에 약간의 기능을 부여했습니다; Move_Up, Move_Down 및 Delete. 삭제 작업이 있지만 다른 두 작업을 수행하는 방법을 알 수 없습니다. 지금 내가 시작과 가고있는 텍스트의 행의 끝을 참조하는 두 값으로 작업하고있어 수정할 수 : self.line_start 여기 self.line_endTkinter 텍스트 위젯에서 라인 바꾸기

그리고 내가 지금까지 가지고 무엇 : 기본적으로

def Move_Up(self): 
    self.TextWidg.config(state='normal') 
    #swap this line with the line above it 
    self.TextWidg.config(state='disabled') 

def Move_Down(self): 
    self.TextWidg.config(state='normal') 
    #swap this line with the line below it 
    self.TextWidg.config(state='disabled') 

def Delete(self): 
    self.TextWidg.config(state='normal') 
    #delete the line 
    self.TextWidg.delete(self.line_start,self.line_end) 
    #delete the carriage return 
    self.TextWidg.delete(self.line_start) 
    self.TextWidg.config(state='disabled') 

, 내 값 self.line_startself.line_end을 구현하여 그 행을 행 앞 또는 행 뒤의 행으로 바꾸는 방법은 무엇입니까?

답변

1

index 메소드를 사용하여 위젯에서 어떤 위치의 색인을 얻을 수 있습니다. linestartlineend과 같은 수정자를 포함하는 인수를 제공 할 수 있습니다. 다음 문자의 색인을 얻으려면 +1c과 같은 상대 위치의 색인을 가져 오거나 +1l을 사용하여 다음 줄을 가져올 수도 있습니다. wordstartwordend을 사용할 수도 있습니다.

: index("insert lineend +1c")

예를 들어, 시작과 삽입 커서가있는 라인의 끝을 얻기 위해 (여기서 '삽입'삽입 커서를 나타내는 마크의 이름입니다) : 당신은 예를 들어, 그것들을 결합 할 수 있습니다

start = self.TextWidg("insert linestart") 
end = self.TextWidg("insert lineend") 

자세한 내용은 text widget page on effbot.org의 "expressions"절을 참조하십시오.

2

브라이언의 제안에 따라 다음과 같이 Move_Up() 및 Move_Down() 메서드를 해결할 수있었습니다. 그 라인에 텍스트를 추가합니다 텍스트 Move_Up()의 한 줄이 경우 참고 그것은 그 : 그것은 맥 OS X의에

#swap this line with the line above it 
def Move_Up(): 
    text.config(state='normal') 
    # get text on current and previous lines 
    lineText = text.get("insert linestart", "insert lineend") 
    prevLineText = text.get("insert linestart -1 line", "insert -1 line lineend") 

    # delete the old lines 
    text.delete("insert linestart -1 line", "insert -1 line lineend") 
    text.delete("insert linestart", "insert lineend") 

    # insert lines in swapped order 
    text.insert("insert linestart -1 line", lineText) 
    text.insert("insert linestart", prevLineText) 
    #text.config(state='disabled') 


#swap this line with the line below it 
def Move_Down(): 
    text.config(state='normal') 
    # get text on current and next lines 
    lineText = text.get("insert linestart", "insert lineend") 
    nextLineText = text.get("insert +1 line linestart", "insert +1 line lineend") 

    # delete text on current and next lines 
    text.delete("insert linestart", "insert lineend") 
    text.delete("insert +1 line linestart", "insert +1 line lineend") 

    # insert text in swapped order 
    text.insert("insert linestart", nextLineText) 
    text.insert("insert linestart + 1 line", lineText) 
    #text.config(state='disabled') 

편집을 파이썬 3.1.3이나 2.6.6을 사용하여 작동합니다. Move_Down()은 한 줄만 있으면 아무 작업도 수행하지 않습니다.

+0

Move_Up 당신의 광고 중복 피하기 위해 다음 사용할 수있다 ("삽입")를 'POS = text.index을 pos.split 경우 (".") [0] == '1': 창 ' – Eruvanos

관련 문제