当前位置: 动力学知识库 > 问答 > 编程问答 >

tkinter Text editor lines counter

问题描述:

*I am not so good at English.

So please be good to understand my awkward English.

Hello!

I am making a Text editor.

but, I want to be more perfect...

so I want to making text line counter.

this look:

what can I do To use this feature?

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)

Full Source code download -> http://blog.naver.com/tdh8316/220854695216

This code has encountered an error.

Error message is

...

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

Why Error Causes?

What is 'method object'?

I can't speak english very well. Sorry,

Thank you

网友答案:

you can create a Text widget, pack it on the LEFT with fill = Y. Here is the example code :

...#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)
分享给朋友:
您可能感兴趣的文章:
随机阅读: