-1

I can't for the life of me figure out why this is not working? I'm just trying to get a canvas to scroll with scrollbars.

I've followed @BryanOakley's advice to the question Tkinter Scrollbar not working but I can't seem to figure out what I am doing wrong.

Here is my code:

import tkinter as tk

class Application(tk.Frame):

    def __init__(self, master=None):
        super().__init__(master, bg= "#E3E5E6")
        self.master = master
        self.grid(sticky = "nesw")
        self.grid_rowconfigure(1, weight=1)
        self.grid_columnconfigure(0, weight=1) 

        self.canvas = tk.Canvas(master)
        self.canvas.create_oval(10, 10, 20, 20, fill="red")
        self.canvas.create_oval(200, 200, 220, 220, fill="blue")
        self.canvas.grid(row=0, column=0, sticky = "nesw")

        self.scroll_x = tk.Scrollbar(master, orient="horizontal", command=self.canvas.xview)
        self.scroll_x.grid(row=1, column=0, sticky="ew")

        self.scroll_y = tk.Scrollbar(master, orient="vertical", command=self.canvas.yview)
        self.scroll_y.grid(row=0, column=1, sticky="ns")

        self.canvas.configure(yscrollcommand=self.scroll_y.set, xscrollcommand=self.scroll_x.set)
        self.canvas.configure(scrollregion=self.canvas.bbox((0,0,15000,15000)))

if __name__ == "__main__":
    root = tk.Tk()

    root.grid_rowconfigure(0, weight=1)
    root.grid_columnconfigure(0, weight=1)
    root.geometry("{}x{}+0+0".format(600,400))

    app = Application(master=root)
    app.mainloop()

Any help is much appreciated.

Jadon Erwin
  • 551
  • 5
  • 25

1 Answers1

1

Change this:

self.canvas.configure(scrollregion=self.canvas.bbox((0,0,15000,15000)))

To this:

self.canvas.configure(scrollregion=(0,0,15000,15000))

The scrollregion attribute requires a tuple of four coordinates. Calling bbox can return that tuple, but only if you give it an item id or a tag. You were feeding it a tuple, and since there was no item on the canvas that has a tag that looked like the tuple it was returning None.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685