Search code examples
pythonmatplotlibtkinterscrollbarfigure

Scrollbar not functioning on Tkinter Figure Canvas


I realize there are similar questions already posted, but I have attempted to follow each solution with no luck.

I have a figure created using matplotlib that has a variable amount of subplots. This figure is large and so many of the subplots are not visible on the page.

I'm trying to add a scrollbar to allow the user to view the additional subplots. The scrollbar appears just as I expected it to, but it does not function at all.

Below is a portion of the code for this page.

class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        canvas = FigureCanvasTkAgg(f,self)
        canvas.draw()

        scroll=Scrollbar(self, orient = VERTICAL)
        scroll.pack(side = RIGHT, fill=tk.BOTH)

        canvas.get_tk_widget().config(yscrollcommand=scroll.set)
        scroll.config(command=canvas.get_tk_widget().yview)

        canvas.get_tk_widget().pack(side=tk.TOP,fill=tk.BOTH,expand = True)

Here is the result:

Figure Canvas with Scrollbar


Solution

  • I was able to fix the scrollbar by configuring the scroll region using the following:

    canvas.get_tk_widget().config(scrollregion=(0,0,1000,10000))
    

    However, when I scroll now, the figure is cut off where the bottom of the page used to be (See Image):

    Image

    I'm now trying to figure out how to extend the FigureCanvasTkAgg to be able to view all of the subplots.