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

前端 未结 1 1381
自闭症患者
自闭症患者 2021-01-04 22:40

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 o

相关标签:
1条回答
  • 2021-01-04 22:50

    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 
    
    0 讨论(0)
提交回复
热议问题