Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

autoscroll of text and scrollbar in python text box

Tags:

python

tkinter

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?

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

Thanks in advance,

like image 682
Ds Arjun Avatar asked Jun 05 '15 14:06

Ds Arjun


People also ask

How do I add a scrollbar to a text box in Python?

By default, the vertical scrollbars are available in the constructor and we don't need to have an orientation for the scrollbar. To attach a vertical scrollbar in a Tkinter text widget, you can use xscrollcommand and yscrollcommmand to set the value of vertical and horizontal scrollbars.

How to add a scroll bar to a text widget Tkinter?

Adding a scrollbar to our Text Widget To create a scrollbar object, use tk. Scrollbar() and add it to our application! Now, after you pack it to the application, we can display longer texts using a scrolling text widget! You can see the scrollbar to the right, supported by the text widget to the left.


2 Answers

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

For example, to make the last line of the widget visible you can use the 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()
like image 60
Bryan Oakley Avatar answered Sep 22 '22 14:09

Bryan Oakley


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

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

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

like image 43
Mike from PSG Avatar answered Sep 21 '22 14:09

Mike from PSG