Skip to content Skip to sidebar Skip to footer

Tkinter Canvas Scrollbar Greyed Out

I have tried following a few tutorials for how to get a canvas to scroll however it always ends up being greyed out. I tried adding a scrollregion to the canvas but I didn't unders

Solution 1:

You should not use pack() to put the frame self.left into the self.canvas, use self.canvas.create_window(0, 0, window=self.left, anchor='nw') instead.

Also you should not put the scrollbar into the frame self.left, put it into the frame self.bottom instead and pack it to side LEFT.

Finally you need to update scrollregion of self.canvas in order to make the scrollbar work.

Below is the modified code based on yours:

    self.main_window = Tk()
    self.root = Frame(self.main_window, bg="white")
    self.root.pack(fill=BOTH, expand=1)

    # TOP SECTION
    self.top = Frame(self.root, bg="white")
    self.top.pack(fill=BOTH, expand=1)
    Label(self.top, text="#########################################").pack()

    # BOTTOM SECTION
    self.bottom = Frame(self.root, bg="white")
    self.bottom.pack(fill=BOTH, expand=1)

    # BOTTOM-LEFT SECTION
    self.canvas = Canvas(self.bottom, bg="white")
    self.canvas.pack(fill=BOTH, expand=1, side=LEFT)
    self.left = Frame(self.canvas)
    #self.left.pack(fill=BOTH, expand=1)
    self.canvas.create_window(0, 0, window=self.left, anchor='nw') ###
    left_scroll = Scrollbar(self.bottom, orient=VERTICAL) ### self.left to self.bottom
    left_scroll.pack(side=LEFT, fill=Y) ### side=RIGHT to side=LEFT
    left_scroll.config(command=self.canvas.yview)
    self.canvas.configure(yscrollcommand=left_scroll.set)
    for root, dirs, files in os.walk("C:\\", topdown=True):
        full = dirs + files
        for i in full:
            Button(self.left, text=i, bg="white", anchor="w", relief=SOLID, borderwidth=0).pack(fill=BOTH)
        break
    ### update scrollregion of self.canvas
    self.left.update()
    self.canvas.configure(scrollregion=self.canvas.bbox('all'))

    # BOTTOM-RIGHT SECTION
    self.right = Frame(self.bottom, bg="white")
    self.right.pack(fill=BOTH, expand=1, side=RIGHT)
    Label(self.right, text="##########################").pack()
    Label(self.right, text="##########################").pack()

    self.main_window.mainloop()

Post a Comment for "Tkinter Canvas Scrollbar Greyed Out"