如何使用Tkinter更新滚动条位置



我正在尝试使用Tkinter为python脚本创建一个GUI,并有一个可工作的滚动条。但是,当我滚动或拖动条形图时,"条形图"的位置不会更新。我认为代码的相关部分如下:

#Setup window with text and scrollbar       
root = Tk()
scrollbar = Scrollbar(root)
app = App(root)
t = Text(root)
#GRID manager layout
t.grid(row = 0, column = 1, sticky=N+S+W, padx = 5, pady = 5)
scrollbar.grid(row = 0, column = 2, sticky=N+S+W, )
scrollbar.config( command = t.yview )

我试着寻找解决这个问题的方法,但似乎无法弄清楚我做错了什么。如有任何帮助,我们将不胜感激。如果我没有包含足够的代码,如果你想要更多,或者想看完整的脚本(尽管有100行(,我很抱歉,我很乐意帮忙。

再次感谢您抽出时间。

您应该将其指向Canvas的.yview,并将Text放入画布中

the_window = Tk()
vscrollbar = Scrollbar(the_window)
vscrollbar.grid(...)
the_canvas = Canvas(
    the_window,
    background = 'white',
    yscrollcommand = vscrollbar.set
)
the_canvas.grid(...)
vscrollbar.config(command=the_canvas.yview)

最新更新