tkinter scrollbar inactive until window resized

Question:

I created a tkinter app that shows a few matplotlib graph. Everything is working fine but the scrollbar is inactive until I resize the window. As soon as I manually resize the window it works like a charm…

Here is the pertinent code:

main_frame=tkinter.Frame(root)
main_frame.pack(fill=tkinter.BOTH, expand=1, pady=(4,0))
my_canvas =tkinter.Canvas(main_frame)
my_canvas.pack(side=tkinter.LEFT, fill=tkinter.BOTH, expand=1)
my_scrollbar=ttk.Scrollbar(main_frame, orient=tkinter.VERTICAL, command=my_canvas.yview)
my_scrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y)
my_canvas.configure(yscrollcommand=my_scrollbar.set)
my_canvas.bind('<Configure>', lambda e: my_canvas.configure(scrollregion=my_canvas.bbox("all")))
second_frame=tkinter.Frame(my_canvas)
my_canvas.create_window((0,0), window=second_frame, anchor="nw")

enter image description here

Asked By: Fred Dubois

||

Answers:

You need to configure the scrollregion of the canvas. You only do that from a binding on <Configure> which won’t fire until the canvas resizes.

Add the following to your code after you call create_window and have added whatever widgets you’re going to add in second_frame:

my_canvas.configure(scrollregion=my_canvas.bbox("all"))
Answered By: Bryan Oakley
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.