Friday, 15 May 2015

Python 3 - Tkinter Scrolling with Inserted Text

I am using tkinter's ScrolledText to create a text box that I insert text in each line.

The problem
The problem was that when I inserted new text, the text box would not automatically scroll down to reveal the new text.

The solution
The solution turned out to be the yview method.

In the example below, I added while loop of 100 numbers(lines) to demonstrate that noew the text box is now focussed to the bottom of the text.

###Tkinter Scrolling with Text###
'''Make text scroll to the bottom of the text box as new entries appear'''
from tkinter import *
from tkinter.scrolledtext import *
window = Tk()
window.wm_title("Scroll From Bottom")
TextBox = ScrolledText(window, height='10', width='45', wrap=WORD)
#
#Just adds 100 lines to the TextBox
count = 0
while(count<100):
TextBox.insert(END, "The count is: " +str(count)+"\n")
#Pushes the scrollbar and focus of text to the end of the text input.
TextBox.yview(END)
count += 1
TextBox.pack()
window = mainloop()

No comments:

Post a Comment