考虑以下示例:
from tkinter import *
startingWin = Tk()
canvas = Canvas(startingWin, height=600)
canvas.grid(row=0, column=0,sticky="nsew")
canvasFrame = Frame(canvas)
canvas.create_window(0, 0, window=canvasFrame, anchor='nw')
for i in range(70):
element = Button(canvasFrame, text='Button %s ' % i)
element.grid(row=i, column=0)
yscrollbar = Scrollbar(startingWin, orient=VERTICAL)
yscrollbar.config(command=canvas.yview)
canvas.config(yscrollcommand=yscrollbar.set)
yscrollbar.grid(row=0, column=1, sticky="ns")
canvas.yview_moveto(0.5)
canvasFrame.bind("<Configure>", lambda event: canvas.configure(scrollregion=canvas.bbox("all")))
startingWin.mainloop()
预期输出: 滚动条在中间。
但是,我获得的滚动条 始终在顶部 , 无论 {1}}如下所示:
我该如何解决?
答案 0 :(得分:2)
在定义canvas.yview_moveto(0.5)
之后,您才应该致电scrollregion
。