Canvas 在不该向上滚动的时候向上滚动
Canvas scrolling up when it shouldn't
我有一个canvas
canvas = tk.Canvas(app, bd=0, highlightthickness=0, bg=style.background)
canvas.pack(side="left", fill="both", expand=True)
我有一个滚动条 canvas
def onFrameConfigure(canvas):
# Reset the scroll region to encompass the inner frame
canvas.configure(scrollregion=canvas.bbox("all"))
scrollbar = tk.Scrollbar(app, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=scrollbar.set)
scrollbar.pack(side="right", fill="y")
main_frame.bind("<Configure>", lambda event, canvas=canvas: onFrameConfigure(canvas))
然后我设置上下键上下滚动
app.bind("<Down>", lambda event: canvas.yview_scroll(1, "units"))
app.bind("<Up>", lambda event: canvas.yview_scroll(-1, "units"))
问题是,当 canvas
中的滚动条被禁用时,我可以向上滚动 ,然后我应该能够向上滚动(使用 canvas.yview_scroll(-1, "units")
).
应该是这样的,除非有东西要向上滚动,否则不让我向上滚动。
即使我这样做它也应该保持这样 canvas.yview_scroll(-1, "units")
,它仍然不应该像第一张图片那样向上滚动
还是这样,当滚动条处于活动状态时,我无法向上滚动。
如果我这样做 canvas.yview_scroll(-1, "units")
,什么都不会发生,因为我已经在顶部。我也希望在第一种情况下发生这种情况(当滚动条未激活时)。
基本上,我想要 canvas
没有活动滚动条(因为没有什么可以滚动到),canvas 中的小部件保持在顶部(它不会让你滚动 canvas.yview_scroll(-1, "units")
).
提前致谢!
Question: Canvas scrolling up when it shouldn't
当 scrolledregion=
小于 canvas.
的父级时会发生这种情况,此处为 app
。
您正在使用:
bbox(item=None)
Returns the bounding box, as a 4-tuple, for all matching items.
如果没有项或所有项适合父项height
,bbox
可以更小。
解决方案:
在应用 scrolledregion=
之前验证 bbox
,这里是 height
值,比父级的 height
>=
。
def onFrameConfigure(canvas):
# Reset the scroll region to encompass the inner frame
bbox = self.canvas.bbox("all")
x, y, width, height = bbox
if height < self.canvas.winfo_height():
bbox = x, y, width, self.canvas.winfo_height()
self.canvas.configure(scrollregion=bbox)
服从
的注意事项
- Tkinter.Canvas.bbox-method
the bounding box is approximate and may differ a few pixels from the real value
我有一个canvas
canvas = tk.Canvas(app, bd=0, highlightthickness=0, bg=style.background)
canvas.pack(side="left", fill="both", expand=True)
我有一个滚动条 canvas
def onFrameConfigure(canvas):
# Reset the scroll region to encompass the inner frame
canvas.configure(scrollregion=canvas.bbox("all"))
scrollbar = tk.Scrollbar(app, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=scrollbar.set)
scrollbar.pack(side="right", fill="y")
main_frame.bind("<Configure>", lambda event, canvas=canvas: onFrameConfigure(canvas))
然后我设置上下键上下滚动
app.bind("<Down>", lambda event: canvas.yview_scroll(1, "units"))
app.bind("<Up>", lambda event: canvas.yview_scroll(-1, "units"))
问题是,当 canvas
中的滚动条被禁用时,我可以向上滚动 ,然后我应该能够向上滚动(使用 canvas.yview_scroll(-1, "units")
).
应该是这样的,除非有东西要向上滚动,否则不让我向上滚动。
即使我这样做它也应该保持这样 canvas.yview_scroll(-1, "units")
,它仍然不应该像第一张图片那样向上滚动
还是这样,当滚动条处于活动状态时,我无法向上滚动。
如果我这样做 canvas.yview_scroll(-1, "units")
,什么都不会发生,因为我已经在顶部。我也希望在第一种情况下发生这种情况(当滚动条未激活时)。
基本上,我想要 canvas
没有活动滚动条(因为没有什么可以滚动到),canvas 中的小部件保持在顶部(它不会让你滚动 canvas.yview_scroll(-1, "units")
).
提前致谢!
Question: Canvas scrolling up when it shouldn't
当 scrolledregion=
小于 canvas.
app
。
您正在使用:
bbox(item=None)
Returns the bounding box, as a 4-tuple, for all matching items.
如果没有项或所有项适合父项height
,bbox
可以更小。
解决方案:
在应用 scrolledregion=
之前验证 bbox
,这里是 height
值,比父级的 height
>=
。
def onFrameConfigure(canvas):
# Reset the scroll region to encompass the inner frame
bbox = self.canvas.bbox("all")
x, y, width, height = bbox
if height < self.canvas.winfo_height():
bbox = x, y, width, self.canvas.winfo_height()
self.canvas.configure(scrollregion=bbox)
服从
的注意事项
- Tkinter.Canvas.bbox-method
the bounding box is approximate and may differ a few pixels from the real value