简体   繁体   中英

Python Tkinter Text-Widget scroll to specific line

Issue:

  • Loading a code-file from the harddrive into a text-widget created by tkinter (may be larger than xxx lines)
  • Finding a position within this text-widget (line/column)
  • Scrolling the text-widget to that exact position

So far I could not find any way to scroll to a specific line in a Tkinter text-widget. Since Tkinter always uses the line/column (eg "1.0") notation to identify positions in the text-widget, isn't there a way to directly scroll to a given position?

So far I tried setting the yview directly:

textWidget.yview("scroll", scrollamount, "units")

But that seems very unclean to me to use the "units" here...

Looking for a clean solution. Thanks.

>>> help(Tkinter.Text.see)
Help on method see in module Tkinter:

see(self, index) unbound Tkinter.Text method
    Scroll such that the character at INDEX is visible.

To make sure line 3 is visible do:

textWidget.see('3.0')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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