我有画布。画布内部是一个框架。在那个框架里面是一个标签。 随着标签中文本的增长,画布的大小也会增加。我已经设置了一个垂直滚动条,允许我查看溢出窗口大小的文本。滚动条按预期工作,但我希望它随着文本的增长自动滚动到底部(即保持锁定到底部)。我怎么能这样做?
使用canvas.yview_moveto( 1 )
无效......
以下是我的代码:
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()
答案 0 :(得分:2)
我找到了解决方案here。 canvas.yview_moveto( 1 )
似乎无所事事,因为我在安装过程中只调用了一次。为了使“自动”滚动起作用,我需要在每次文本增长时调用它。