Monday, August 18, 2008

Text box with scrollbar

A very nice example can be found here.
I you want to move focus so that the last entry in the text box will be visible, you may modify the code as follows:

import Tkinter

root = Tkinter.Tk()
s = Tkinter.Scrollbar(root)
T = Tkinter.Text(root)

s.pack(side=Tkinter.RIGHT, fill=Tkinter.Y)
T.pack(side=Tkinter.LEFT, fill=Tkinter.Y)

for i in range(40):
T.insert(Tkinter.END, "This is line %d\n" % i)
T.yview(Tkinter.MOVETO, 1.0)


The 1.0 parameter of .yview means the end of the Text widget contents.
I'm usually using the Grid geometry manager and aligning the scrollbar with it requires some more work than with pack. Nothing out of ordinary though.

1 comment:

Anonymous said...