2016-11-06 1 views
0

* 영어가 너무 좋지 않습니다. 제 영어를 이해하는 것이 좋습니다.tkinter 텍스트 편집기 행 카운터

안녕하세요!

텍스트 편집기를 만들고 있습니다.

하지만, 좀 더 완벽 할이 ...

그래서 텍스트 라인 카운터를 만들고 싶어요. 이 Look :

enter image description here

나는이 기능을 사용하기 위해 무엇을 할 수 있는가?

class main: 
    def __init__(self,master): 
     (skip) 
     self.__class__.editors.append(self) 
    self.lineNumbers = '' 
    self.frame = Frame(master, bd=2, relief=SUNKEN) 
    self.lnText = Text(self.frame, 
      width = 4, 
      padx = 4, 
      highlightthickness = 0, 
      takefocus = 0, 
      bd = 0, 
      background = 'lightgrey', 
      foreground = 'magenta', 
      state='disabled' 
    ) 
    self.lnText.pack(side=LEFT, fill='y') 
    if self.__class__.updateId is None: 
     self.updateAllLineNumbers() 

    def getLineNumbers(self): 
    x = 0 
    line = '0' 
    col= '' 
    ln = '' 
    # assume each line is at least 6 pixels high 
    step = 6 
    nl = '\n' 
    lineMask = ' %s\n' 
    indexMask = '@0,%d' 
    for i in range(0, self.editor.winfo_height, step): 
     ll, cc = self.editor.index(indexMask % i).split('.') 
     if line == ll: 
      if col != cc: 
       col = cc 
       ln += nl 
     else: 
      line, col = ll, cc 
      ln += (lineMask % line)[-5:] 
    return ln 

def updateLineNumbers(self): 
    tt = self.lnText 
    ln = self.getLineNumbers() 
    if self.lineNumbers != ln: 
     self.lineNumbers = ln 
     tt.config(state='normal') 
     tt.delete('1.0', END) 
     tt.insert('1.0', self.lineNumbers) 
     tt.config(state='disabled') 

@classmethod 
def updateAllLineNumbers(cls): 
    if len(cls.editors) < 1: 
     cls.updateId = None 
     return 
    for ed in cls.editors: 
     ed.updateLineNumbers() 
    cls.updateId = ed.text.after(
     cls.UPDATE_PERIOD, 
     cls.updateAllLineNumbers) 

전체 소스 코드를 다운로드 ->http://blog.naver.com/tdh8316/220854695216

이 코드는 오류가 발생했습니다.

오류 메시지가

... 
Traceback (most recent call last): 
    File "G:\_#Project\_Editor\py\TextMate\TextMate\TextMate.py", line 547, in  <module> 
    runMainWindow() # Run MainWindow Class 
    File "G:\_#Project\_Editor\py\TextMate\TextMate\TextMate.py", line 543, in runMainWindow 
    app = MainWindow(root) 
    File "G:\_#Project\_Editor\py\TextMate\TextMate\TextMate.py", line 68, in __init__ 
    self.updateAllLineNumbers() 
    File "G:\_#Project\_Editor\py\TextMate\TextMate\TextMate.py", line 532, in  updateAllLineNumbers 
    ed.updateLineNumbers() 
    File "G:\_#Project\_Editor\py\TextMate\TextMate\TextMate.py", line 516, in updateLineNumbers 
    ln = self.getLineNumbers() 
    File "G:\_#Project\_Editor\py\TextMate\TextMate\TextMate.py", line 499, in getLineNumbers 
    for i in range(0, self.editor.winfo_height, step): 
TypeError: 'method' object cannot be interpreted as an integer 

왜 오류 원인인가?

'메서드 개체'란 무엇입니까?

저는 영어를 잘 못합니다. 죄송합니다.

+0

전체 소스 코드 : http://blogattach.naver.com/60f57cccdc8184587695fbc6fe196519bae012f5d8/20161106_97_blogfile/tdh8316_1478408090149_A7lwVg_py/TextMate.py?type=attachment – tdh8316

+0

'self.editor.winfo_height'가 함수 인 다음 예제 코드는 . 그 함수의 결과를 원하면'self.editor.winfo_height()'라고 부를 필요가있다. –

+0

고마워요! 하지만 다른 곳에서 오류가 발생하는 ... 아직 해결할 것입니다 .......! :-) – tdh8316

답변

0

Text 위젯을 만들려면 LEFTfill = Y으로 포장하십시오.

...#some stuff above including text 

lineText = Text() 
lineText.config(state = DISABLED) 

#we need to make sure that the scrollbar on your text widget also scrolls the lineText 
def configScroll(*args): 
    textBlock.yview(*args) 
    lineText.yview(*args) 

scrollBarOnYourTextWidget.config(command = configScroll) 

def updateLineNumber(): 
    endOfText = int(textBlock.index("end").split(".")[0]) #get where we need to stop 
    for i in range(1, endOfText + 1): 
     lineText.config(state = NORMAL) 
     lineText.insert(END, i) 
     lineText.config(state = DISABLED) 

    #Config the width of the lineText 

    lineText.config(width = len(endOfText) + 2) #just a formula I found :) 

    #Next we need to make sure that when you type, the lineText follows you 

    textBlock.mark_set("TextMark", endOfText) 
    #Scroll to that spot 

    textBlock.see("TextMark") 

textBlock.bind("<Any-Key>", updateLineNumber) 
+0

오, 고맙습니다. 나는이 예를 시도 할 것이다. – tdh8316

+0

성공! 고맙습니다. 나는 너를 영원히 잊지 않을 것이다 .. – tdh8316