Troy Rockwood
Troy Rockwood

Reputation: 6147

Python Tkinter Scrollable frame has a scrollbar but doesn't scroll

I have a list of completed or continuing simulations that the user can select via a checkbutton and compare with one another. The list recently grew beyond the size of the screen and so I would like to implement a scrollbar. I implemented the window as a Toplevel and following an example on stackOverflow put a frame in the Toplevel with a canvas in that with a frame in that which contains the grid-based array of checkboxes and labels. The scroll bar shows on the right but you can't scroll. Here is my code:

 def overview_callback(self):
    self.overviewWindow = Toplevel(self.master)
    self.overviewWindow.geometry("1500x500")
    self.overviewFrame = Frame(self.overviewWindow)
    self.overviewFrame.pack(side=TOP, fill="both", expand=True)
    self.overviewCanvas = Canvas(self.overviewFrame)
    self.insideFrame = Frame(self.overviewCanvas)
    self.hsb = Scrollbar(self.overviewFrame, orient="horizontal", command=self.overviewCanvas.xview)
    self.hsb.pack(side=BOTTOM, fill="x")
    self.overviewCanvas.configure(xscrollcommand=self.hsb.set)

    self.vsb = Scrollbar(self.overviewFrame, orient="vertical", command=self.overviewCanvas.yview)
    self.overviewCanvas.configure(yscrollcommand=self.vsb.set)
    self.vsb.pack(side=RIGHT, fill="y")
    self.overviewCanvas.pack(side=TOP, fill="both", expand=True)
    self.overviewCanvas.create_window((0,0), window=self.insideFrame, anchor="nw", tags="self.insideFrame")
    self.overviewCanvas.pack(side=TOP, fill="both", expand=True)
    self.insideFrame.bind("<Configure>", self.onFrameConfigure)

    self.overviewWindow.title("Simulations Overview")
    self.overviewList = []
    self.overviewVariables = []
    count = 0
    for geometry in self.gNames:
        goalNames = os.listdir('Geometries/'+geometry+'/Goals')
        if len(goalNames) > 0:
            for l, goal in enumerate(goalNames):
                count += 1
                self.overviewList.append([geometry, goal])
                self.overviewVariables.append(IntVar())
                Checkbutton(self.insideFrame, variable=self.overviewVariables[-1]).grid(row=count, column=0, sticky=W)
                Label(self.insideFrame, text=geometry+","+goal+":").grid(row=count, column=1, sticky=W)
                simStatus, simType, latestResult = tuneUtils.getLatestResult(goal, 'Geometries/'+geometry+'/Goals/'+goal+"/Output.txt")
                if simStatus == "Not Started":
                    Label(self.insideFrame, text=simStatus).grid(row=count, column=2, sticky=W)
                else:
                    # Extract Within/Not Within and GlobalMetric number to display
                    upToNotWithin = re.findall("^.*"+"not within Tolerance: ["+"[\w\s]*\]",latestResult)[0]
                    score = re.findall("GlobalMetric = "+"[\s\-\d\.]*", latestResult)[0]
                    Label(self.insideFrame, text=simStatus+" "+simType+" "+upToNotWithin+" "+score).grid(row=count, column=2, sticky=W)
    button_frame = Frame(self.overviewWindow)
    button_frame.pack(side=BOTTOM)
    exit_button = Button(button_frame, text='Exit', command=self.overviewExit_callback)
    compare_button = Button(button_frame, text='Compare', command=self.overviewCompare_callback)
    for b in (exit_button, compare_button):
        b.pack(side=LEFT, expand=YES, padx=10, pady=10, fill=BOTH)

def onFrameConfigure(self, event):
    self.overviewCanvas.configure(scrollregion=self.overviewCanvas.bbox("all"))

Thank you for your help.

I have made a number of edits in order to make the windows resizable and to put the scrollbars in the correct spots. Thanks for the help.

Upvotes: 0

Views: 229

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386255

You cannot use grid to place a widget inside a canvas if you expect that widget to scroll. You must use the create_window method of the canvas. Try removing this statement:

self.insideFrame.grid()

Upvotes: 1

Related Questions