Dvlv/Tkinter-By-Example

Scrollbar Problem

Closed this issue · 1 comments

not94 commented

Hello.After following the ToDoList V2 guide, I found something weird about the scrollbar.When the task items are too few to show the scrollbar, I can still scroll the task item because of listening the mousewheel event, which make the task frame move up and down.
I tried to find some solution on stackoverflow and the situation is similar to this question.
If I don't explain clearly, you can see the comments under the stackoverflow answer, which is the problem I meet and the solution does not work.Maybe you can figure out this problem and make the code more perfect.
Thanks a lot!

Dvlv commented

Hi, thanks for the freedback.

This is the fix:

    def on_frame_configure(self, event=None):
        region = list(self.tasks_canvas.bbox("all"))
        if self.tasks_canvas.winfo_height() > region[3]:
            region[3] = self.tasks_canvas.winfo_height()
        self.tasks_canvas.configure(scrollregion=region)

The scrollregion of the canvas is small when there are few items in it, so it allows them to be scrolled around the canvas.

If the height of the contained items is smaller than the total height of the canvas, we can set the scrollregion to be that of the entire canvas height, meaning it won't let us scroll the items to the bottom unnecessarily.

When the embedded frame exceeds the height of the canvas, we are then able to scroll the elements around.

I no longer have the machine which I wrote this book on so it may be a while before I put this fix into it.