Python Tkinter: Canvas scrolling with MouseWheel

北慕城南 提交于 2019-12-01 11:21:56

You can use the bbox method of canvas to retrieve the actual height of drawn items on canvas. bbox return a tuple defining a rectangle. You can compare it with the height of your canvas widget.

  height = self.canvas.winfo_height()
  _,_,_,items_height = self.canvas.bbox(Tkinter.ALL)
  if (items_height < height):
     direction = 0

I'm pretty unfamiliar with Trees, but it looks like it's just a bunch of Labels. You can measure their heights and compare them with the height of the Canvas to determine if you need to scroll. This is a little sloppy, but it worked for me:

if self.canvas.winfo_reqheight() < len(self.canvas.winfo_children()) * self.canvas.winfo_children()[0].winfo_reqheight():
    self.canvas.yview_scroll(direction, "units")
else:
    pass

EDIT: in case that's too messy, here's the pseudo code:

if CANVAS_HEIGHT < NUMBER_OF_LABELS * LABEL_HEIGHT:
    scroll
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!