*我不擅長英語。 所以,請好理解我的尷尬英語。tkinter文本編輯器行計數器
您好!
我在做一個文本編輯器。
,但我想更完美......
所以我想使文本行計數器。 這一下:
我能做些什麼要使用此功能?
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
爲什麼錯誤原因?
什麼是'方法對象'?
我不會說英語很好。對不起, 謝謝
完整源代碼:http://blogattach.naver.com/60f57cccdc8184587695fbc6fe196519bae012f5d8/20161106_97_blogfile/tdh8316_1478408090149_A7lwVg_py/TextMate.py?type=attachment – tdh8316
'self.editor.winfo_height'是一個函數。如果你想得到這個函數的結果,你需要調用它:'self.editor.winfo_height()'。 –
謝謝!但要在其他地方發生錯誤......但我會解決這個.......! :-) – tdh8316