Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

like image 883
rectangletangle Avatar asked Jan 05 '11 21:01

rectangletangle


1 Answers

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 

Update per Bryan Oakley's comment:

>>> int(text_widget.index('end-1c').split('.')[0])  # returns line count
2 
like image 117
Steven Rumbalski Avatar answered Sep 22 '22 14:09

Steven Rumbalski