简体   繁体   English

获取Tkinter Text小部件中的总行数?

[英]Getting the total number of lines in a Tkinter Text widget?

I have a Tkinter Text widget, and I'd like to know how many lines it contains. 我有一个Tkinter Text小部件,我想知道它包含多少行。

I know of the text.cget("height") method, however that only tells me how many lines are displayed. 我知道text.cget("height")方法,但只告诉我显示了多少行。 I'd like to know how many lines there are total. 我想知道总共有多少行。

I'm using this info to try to make my own custom scrollbar, so any help would be much appreciated. 我正在使用此信息尝试制作我自己的自定义滚动条,所以任何帮助将非常感激。

Use the index method to find the value of 'end' which is the position just after the last character in the buffer. 使用index方法查找'end'的值,它是缓冲区中最后一个字符之后的位置。

>>> text_widget.index('end')  # returns line.column 
'3.0'

>>> int(text_widget.index('end').split('.')[0]) - 1  # returns line count
2 

Update per Bryan Oakley's comment: 根据Bryan Oakley的评论更新

>>> int(text_widget.index('end-1c').split('.')[0])  # returns line count
2 

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

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