Visible lines in Tkinter text widget
Is there any way to get what lines are currently visible in a Tkinter text widget? I'm trying to make a scrollbar.
Currently I'm trying to coordinate my scroll 开发者_JAVA百科bar with the other methods of scrolling (ie, the arrow keys and scroll wheel). So that when I scroll with the arrow keys or mouse, my bar scrolls too.
You can get the character (and thus, the line number) at a pixel position using an index of @x,y
(eg: @0,0
for the top-left-most visible character). Since you know the width and height of the widget (using the winfo_width
and winfo height
methods) you can calculate what lines are at the bottom of the widget too.
You can also use the yview
method to get the relative portion of the text that is in view (eg: a value of .5 means you are half way through the document)
Why make your own scrollbar? Just use the one already there: http://www.tkdocs.com/tutorial/morewidgets.html
Basically you use the yview/xview methods of a widget to find out whats visible, see the documentation of yview for example. http://www.tcl.tk/man/tcl8.4/TkCmd/text.htm#M146
For more details about scrollbars see: http://wiki.tcl.tk/1455
Please see http://effbot.org/zone/tkinter-scrollbar-patterns.htm
This has information about how the Scrollbar class works.
If you wanted to implement your own scrollbar you just need to send messages to the text box with what position you're at in the range (0,1) and it will return a message telling what range it's targeting.
精彩评论