-1

I'm trying to implement a scrollbar that will scroll through a frame in a canvas window. The canvas, frame, and frame elements are all functioning well as far as I can see. I have no issues with adding or removing elements from the frame, and they show up as I want them to. The only issue is with the scrollbar.

The scrollbar doesn't work at all. It shows up exactly where it should, but it doesn't react at all to the canvas or frame elements. If I resize the window and squish down the canvas, the scrollbar does nothing — it just stays empty as though there's nothing for it to scroll through.

I recognize there are a million other questions about scrollbars in canvases, but I've carefully read through as many of them as I could and didn't find any sort of solution to my problem. So with that, I ask: any idea where I'm going wrong?

Any feedback or suggestions for possible fixes would be greatly appreciated.

lbl_frame = Canvas(menu, bg=root_bg, width=(menu.winfo_width() - 35), height=canvas_size, highlightthickness=0)
f = Frame(lbl_frame, bg=root_bg) # Frame that goes in the canvas

scroll = Scrollbar(menu, bg=root_bg, orient='vertical', width=17) # Scrollbar
scroll.grid(row=3, column=2, sticky='ns')
scroll.config(command=lbl_frame.yview) # Scrollbar's command reads information about the height of the canvas

lbl_frame.create_window(0, 0, window=f, anchor="nw") # Creating a canvas window for the frame
lbl_frame.config(yscrollcommand=scroll.set)
    
def canvas_resize(event):
    global min_size # Currently set to 95
        
    canvas_size = menu.winfo_height() - 170 # Sum of all other elements
        
    if canvas_size < min_size:
        canvas_size = min_size # Canvas size must be >= 100
        
    lbl_frame.configure(height=canvas_size) # Scale canvas size with window
    lbl_frame.configure(scrollregion=lbl_frame.bbox("all"))
    # Set scrollregion to the total region of all elements in the frame
    
menu.bind("<Configure>", lambda e: canvas_resize(e))

Note that elements are added to the frame shortly after this. Because the process I add them by is very long and contains a lot of stuff that isn't relevant to my problem, plus I don't have any issues with adding these elements, I've decided not to include that part.

  • I am pretty sure there was a question that involved using scrollbar and canvas – Matiiss Apr 03 '21 at 22:39
  • does this help? https://stackoverflow.com/questions/7113937/how-do-you-create-a-labelframe-with-a-scrollbar-in-tkinter – Matiiss Apr 03 '21 at 22:43
  • @Matiiss I checked the example code, and as far as I can tell I'm already doing everything that's done there. If you know of any other posts, feel free to link them, though. I hadn't seen that one yet, so it was definitely still good to take a look at! – Skill Decay Apr 03 '21 at 23:34
  • https://stackoverflow.com/questions/3085696/adding-a-scrollbar-to-a-group-of-widgets-in-tkinter here is another question that may help – Matiiss Apr 03 '21 at 23:39
  • Got it figured out. Thanks for the help :) – Skill Decay Apr 03 '21 at 23:59
  • Does this answer your question? [Manually inserting into frame sets scrollbar to not work tkinter](https://stackoverflow.com/questions/65374264/manually-inserting-into-frame-sets-scrollbar-to-not-work-tkinter) – Delrius Euphoria Apr 04 '21 at 04:10

1 Answers1

0

I figured it out. It was with how I was adding the elements. I added them to the canvas, not the frame. So the frame was left empty.