Python:如果用户没有手动滚动,则自动将 ScrolledText 滚动到最后 [英] Python: Scroll a ScrolledText automatically to the end if the user is not scrolling manually

查看:142
本文介绍了Python:如果用户没有手动滚动,则自动将 ScrolledText 滚动到最后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根据 这个使我的 ScrolledText 自动滚动到最后回答.

现在我想自动滚动仅当用户不手动滚动.

我一直在寻找这样的东西:self.text.offsetBottom (见我在下面代码中的评论),但还没有找到.>

有什么想法吗?谢谢!

导入时间从 Tkinter 导入 *导入滚动文本类示例(框架):def __init__(self, *args, **kwargs):Frame.__init__(self, *args, **kwargs)self.text = ScrolledText.ScrolledText(self, height=6, width=40)self.text.pack(side="left", fill="both", expand=True)self.add_timestamp()def add_timestamp(self):self.text.insert("end", time.ctime() + "\n")""" -----> 这里 <----- """# 如果 self.text.offsetBottom >0:self.text.see("end")self.after(1000, self.add_timestamp)如果 __name__ == "__main__":根=Tk()框架 = 示例(根)frame.pack(fill="both", expand=True)root.mainloop()

解决方案

您可以使用 yview() 方法查看小部件是否完全向下滚动.yview() 返回一个 2 元组,其中可见部分的顶部和底部相对于总大小.因此,如果小部件完全向下滚动,则第二个数字应为 1.0.

我们可以使用它来仅在插入发生之前小部件完全向下滚动时滚动:

def add_timestamp(self):full_scrolled_down = self.text.yview()[1] == 1.0self.text.insert("end", time.ctime() + "\n")如果fully_scrolled_down:self.text.see("end")self.after(1000, self.add_timestamp)


另一个选项是使用

检查最后一个字符当前是否可见

visible = self.text.bbox("end-1c")

effbot,我们可以看到这个方法如果字符可见,则给出一个 4 元组,如果字符不可见,则给出 None:

<块引用>

bbox(索引)
计算给定字符的边界框.

此方法仅在文本小部件更新时有效.为确保是这种情况,您可以先调用 update_idletasks 方法.

索引
字符索引.
退货:
一个 4 元组(x、y、宽度、高度),如果字符不可见,则为无.

如果最后一个字符在插入发生之前可见,我们可以使用它来滚动:

def add_timestamp(self):last_char_visible= self.text.bbox(end-1c")self.text.insert("end", time.ctime() + "\n")如果 last_char_visible:self.text.see("end")self.after(1000, self.add_timestamp)

I made my ScrolledText scroll automatically to the end, based on this answer.

Now I'd like to scroll automatically only if the user is not scrolling manually.

I was looking for something like this: self.text.offsetBottom (see my comment in the code below), but couldn't find it yet.

Any ideas? Thanks!

import time
from Tkinter import *
import ScrolledText

class Example(Frame):
    def __init__(self, *args, **kwargs):
        Frame.__init__(self, *args, **kwargs)

        self.text = ScrolledText.ScrolledText(self, height=6, width=40)
        self.text.pack(side="left", fill="both", expand=True)
        self.add_timestamp()

    def add_timestamp(self):
        self.text.insert("end", time.ctime() + "\n")

        """ -----> HERE <----- """         
        # if self.text.offsetBottom > 0:
        self.text.see("end")
        self.after(1000, self.add_timestamp)

if __name__ == "__main__":
    root =Tk()
    frame = Example(root)
    frame.pack(fill="both", expand=True)
    root.mainloop()

解决方案

You can use the yview() method to see if the widget is fully scrolled down. yview() returns a 2-tuple with the top and bottom of the visible part relative to the total size. So if the widget is fully scrolled down, the second number should be 1.0.

We can use this to only scroll if the widget was fully scrolled down before the insert happened:

def add_timestamp(self):
    fully_scrolled_down = self.text.yview()[1] == 1.0
    self.text.insert("end", time.ctime() + "\n")
    if fully_scrolled_down:
        self.text.see("end")
    self.after(1000, self.add_timestamp)


Another option is to check whether the last character is currently visible or not using

visible = self.text.bbox("end-1c")

From effbot, we can read that this method gives a 4-tuple if the character is visible, or None if the character is not visible:

bbox(index)
Calculates the bounding box for the given character.

This method only works if the text widget is updated. To make sure this is the case, you can call the update_idletasks method first.

index
Character index.
Returns:
A 4-tuple (x, y, width, height), or None, if the character is not visible.

We can use this to only scroll if the last character was visible before the insert happened:

def add_timestamp(self):
    last_char_visible= self.text.bbox("end-1c")
    self.text.insert("end", time.ctime() + "\n")
    if last_char_visible:
        self.text.see("end")
    self.after(1000, self.add_timestamp)

这篇关于Python:如果用户没有手动滚动,则自动将 ScrolledText 滚动到最后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆