获取Tkinter文本小部件中的总行数? [英] Getting the total number of lines in a Tkinter Text widget?

查看:645
本文介绍了获取Tkinter文本小部件中的总行数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Tkinter Text小部件,我想知道它包含多少行.

I have a Tkinter Text widget, and I'd like to know how many lines it contains.

我知道text.cget("height")方法,但是只能告诉我显示了多少行.我想知道总共有几行.

I know of the text.cget("height") method, however that only tells me how many lines are displayed. 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.

推荐答案

使用index方法查找'end'的值,该值是缓冲区中最后一个字符之后的位置.

Use the index method to find the value of 'end' which is the position just after the last character in the buffer.

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

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

根据Bryan Oakley的评论

更新:

Update per Bryan Oakley's comment:

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

这篇关于获取Tkinter文本小部件中的总行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆