Python Tkinter: Canvas scrolling with MouseWheel

前端 未结 2 520
鱼传尺愫
鱼传尺愫 2021-01-14 20:47

I have created a tree inside a Canvas, and I have also allowed MouseWheel to scroll up and down.

However, how do I prevent scrolling if tree content has not exceed c

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-14 21:15

    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
    

提交回复
热议问题