python文本框中文本和滚动条的自动滚动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30669015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:47:47  来源:igfitidea点击:

autoscroll of text and scrollbar in python text box

pythontkinter

提问by Ds Arjun

I have a tkinter 'Text' and 'Scrollbar' working fine. In my program in the text window automatically lines will keep on adding. So When a new line of text is inserted and data reached out of limit I would like the text and scrollbar to be scrolled to the bottom automatically, so that the latest line of text is always shown. How to do this?

我有一个 tkinter 'Text' 和 'Scrollbar' 工作正常。在我的程序中,文本窗口中的自动行会不断添加。因此,当插入新的文本行并且数据超出限制时,我希望文本和滚动条自动滚动到底部,以便始终显示最新的文本行。这该怎么做?

Also how to link the scroll of text window and scroll bar, because when I do scrolling over the text window scroll wont happen. Only way I observed is to drag the scroll bar.

还有如何链接文本窗口和滚动条的滚动,因为当我滚动文本窗口时不会发生滚动。我观察到的唯一方法是拖动滚动条。

    scrollbar = Tkinter.Scrollbar(group4.interior())
    scrollbar.pack(side = 'right',fill='y')

    Details1 = Output()        
    outputwindow = Tkinter.Text(group4.interior(), yscrollcommand = scrollbar.set,wrap = "word",width = 200,font = "{Times new Roman} 9")
    outputwindow.pack( side = 'left',fill='y')
    scrollbar.config( command = outputwindow.yview )
    outputwindow.yview('end')
    outputwindow.config(yscrollcommand=scrollbar.set)
    outputwindow.insert('end',Details1)

In the program the function output() will continuously send data, which should scroll

在程序中,函数 output() 将不断发送数据,该数据应滚动

Thanks in advance,

提前致谢,

采纳答案by Bryan Oakley

You can cause the text widget to scroll to any location with the see method, which takes an index.

您可以使用带索引的see 方法使文本小部件滚动到任何位置。

For example, to make the last line of the widget visible you can use the index "end":

例如,要使小部件的最后一行可见,您可以使用 index "end"

outputwindow.see("end")

Here's a complete working example:

这是一个完整的工作示例:

import time
try:
    # python 2.x
    import Tkinter as tk
except ImportError:
    # python 3.x
    import tkinter as tk

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

        self.text = tk.Text(self, height=6, width=40)
        self.vsb = tk.Scrollbar(self, orient="vertical", command=self.text.yview)
        self.text.configure(yscrollcommand=self.vsb.set)
        self.vsb.pack(side="right", fill="y")
        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.see("end")
        self.after(1000, self.add_timestamp)

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

回答by MikeyB

Take a look at Text.see(...)method.

看一下Text.see(...)方法。

TextWidget.insert(tk.END, str(new_txt))
TextWidget.see(tk.END)

I used this pattern to add (aka insert) text new_txtto my output window and scroll (see) to the bottom (tk.END)

我使用此模式将(又名insert)文本添加new_txt到我的输出窗口并滚动 ( see) 到底部 ( tk.END)