Getting the total number of rows in a Tkinter text widget?

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

I know the text.cget("height") method, but it only tells me how many lines are displayed. I would like to know how many lines there are.

I use this information to try to create my own scrollbar, so any help would be greatly appreciated.

+5
source share
1 answer

Use the index method to find the value "end", which is the position immediately 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 for Brian Oakley's comment:

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

All Articles