简体   繁体   English

TK文本小部件索引未正确计数行?

[英]TK text widget index not properly counting lines?

I have tried the code below and used the index method to try to get the number of lines of text contained within the text box but i keep getting 1 line returned instead of the 2 that are present in my text box. 我尝试了下面的代码,并使用index方法尝试获取文本框中包含的文本行数,但我不断返回1行而不是文本框中显示的2行。

from Tkinter import *

master = Tk()

text = Text(master,width=40,height=50,wrap='word')
text.insert('1.0', 'Here is my text to insert, next i want to test the word wrap')
text.tag_add('font', '1.0', 'end')
text.tag_configure('font', font='arial 12', relief='raised')
text.pack()
print int(text.index('end-1c').split('.')[0])

mainloop()

It only counts physical lines, not lines that have wrapped. 它仅计算物理线,而不计算缠绕的线。 You've only inserted one line, so it's only counting one line. 您只插入了一行,所以只算了一行。 Wrapping has no effect on the number of lines. 包装对行数没有影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM