William Gazeley
William Gazeley

Reputation: 181

Tkinter scrollbar is off the screen and doesn't scroll

So I've created a GUI and I need a vertical scrollbar, however the scrollbar seems to be taller than its parent frame, and doesn't scroll. I have no idea why but here's the code I have so far:

import tkinter as Tk
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure

root = Tk.Tk()

RightPane = Tk.Frame(root)
RightPane.grid()

class Graph:
    #Draws and return a placeholder for a graph
    #@param parent is the Tk.Frame parent obj
    #@param title is the (string) title for the graph
    def __init__(self, parent, title=''):
        #Initialise graph
        self.title = title
        self.fig = Figure(figsize=(4,4))
        self.plot = self.fig.add_subplot()
        self.plot.set_title(self.title, fontsize=10)
        self.plot.set_ylim(top=1)
        self.plot.set_xlim(right=255)
        self.plot.set_ylabel("Certainty", fontsize=8)
        self.plot.set_xlabel("Pixel Value", fontsize=8)

        #Draw
        self.canvas = FigureCanvasTkAgg(self.fig, master=parent)
        self.canvas.get_tk_widget().pack()
        self.canvas.draw()

        return

#Result Graphs -------------------------------------------
ResultFrame = Tk.Frame(RightPane)
ResultFrame.grid_columnconfigure(0, weight=1)
ResultFrame.grid(row=2, column=2, rowspan=14, padx=(10,0), pady=(10,0), sticky='nwe')

ResultScrollable = Tk.Canvas(ResultFrame)
ResultScrollable.grid(row=0, column=0, padx=(5,0), sticky='we')

graphCollection = []
for i in range(10):
    title = 'Certainty that image is digit: {}'.format(i)
    graphCollection.append(Graph(ResultScrollable, title=title))

ResultFrameVbar = Tk.Scrollbar(ResultFrame, orient='vertical', command=ResultScrollable.yview)
ResultFrameVbar.grid(row=0, column=1, sticky='nswe')

ResultScrollable.config(yscrollcommand=ResultFrameVbar.set, scrollregion=ResultScrollable.bbox('all'))

root.mainloop()

Couldn't find anything on the internet, so any help would really be appreciated. Thank you in advance.

Upvotes: 0

Views: 245

Answers (1)

acw1668
acw1668

Reputation: 46669

You are creating canvases (self.canvas) inside canvas (ResultScrollable) and using pack() on those canvases. So ResultScrollable.bbox('all') will not include those canvases. You should create an internal Frame and assocate it with create_window(), then put those canvases inside the internal Frame:

internalFrame = Tk.Frame(ResultScrollable)
ResultScrollable.create_window(0, 0, window=internalFrame, anchor='nw')

graphCollection = []
for i in range(10):
    title = 'Certainty that image is digit: {}'.format(i)
    graphCollection.append(Graph(internalFrame, title=title))

Upvotes: 1

Related Questions