2016-11-06 68 views
0

*我不擅長英語。 所以,請好理解我的尷尬英語。tkinter文本編輯器行計數器

您好!

我在做一個文本編輯器。

,但我想更完美......

所以我想使文本行計數器。 這一下:

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