I have a canvas. Inside the canvas is a frame. And inside that frame is a label. As the text in the label grows, the canvas grows in size to accommodate. I've setup a vertical scrollbar, that allows me to see the text that overflows the window size. The scrollbar works as expected, but I want it to automatically scroll to the bottom (i.e. stay locked to the bottom) as the text grows. How can I do this?
Using canvas.yview_moveto( 1 )
does nothing...
Below is my code:
def tk_onFrameConfigure( self, event ):
# resize canvas
self.tkCanvas.configure( scrollregion = self.tkCanvas.bbox( 'all' ) )
def tk_onCanvasConfigure( self, event ):
# resize frame
self.tkCanvas.itemconfigure( self.tkCanvasFrame, width = event.width )
def setupTkinter( self ):
self.tkRoot = tkinter.Tk()
self.tkCanvas = tkinter.Canvas( self.tkRoot )
self.tkCanvas.pack( side = tkinter.LEFT, expand = True, fill = 'both' )
self.tkCanvas.configure(
width = self.width,
height = self.height,
highlightthickness = 0,
bg = self.bgColor
)
scrollbar = tkinter.Scrollbar( self.tkRoot )
scrollbar.pack( side = tkinter.RIGHT, fill = 'y' )
scrollbar.configure(
orient = 'vertical',
command = self.tkCanvas.yview
)
self.tkCanvas.configure( yscrollcommand = scrollbar.set )
# self.tkCanvas.yview_moveto( 1 ) # does nothing
frame = tkinter.Frame( self.tkCanvas )
self.tkCanvasFrame = self.tkCanvas.create_window(
( 0, 0 ),
window = frame,
anchor = 'nw'
)
self.tkTextBox = tkinter.Label( frame )
self.tkTextBox.pack( expand = True, fill = 'both' )
self.tkTextBox[ 'text' ] = self.displayBuffer
self.tkTextBox.config(
fg = self.textColor,
bg = self.bgColor,
anchor = 'nw',
justify = tkinter.LEFT,
wraplength = self.width - 5
)
frame.bind( '<Configure>', self.tk_onFrameConfigure )
self.tkCanvas.bind( '<Configure>', self.tk_onCanvasConfigure )
self.tkRoot.mainloop()
I found the solution here. canvas.yview_moveto( 1 )
appears to do nothing because I only call it once during setup. For the 'auto' scrolling to work, I need to call it each time the text grows.