简体   繁体   中英

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

Try this one. You probably didn't put scroolbar into canvas.

class PageTwo(tk.Frame):

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

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

        scroll=Scrollbar(canvas, 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)

So I changed this. scroll=Scrollbar(self, orient = VERTICAL) To this scroll=Scrollbar(canvas, orient = VERTICAL)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM