开发者

Visible lines in Tkinter text widget

开发者 https://www.devze.com 2023-02-02 10:42 出处:网络
Is there any way to get what lines are currently visible in a Tkinter text widget? I\'m trying to make a scrollbar.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消