tkinter Scrolling widgets Scrolling a Canvas widget horizontally and vertically


The principle is essentially the same as for the Text widget, but a Grid layout is used to put the scrollbars around the widget.

canvas = tk.Canvas(parent, width=150, height=150)
canvas.create_oval(10, 10, 20, 20, fill="red")
canvas.create_oval(200, 200, 220, 220, fill="blue")
canvas.grid(row=0, column=0)

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

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

canvas.configure(yscrollcommand=scroll_y.set, xscrollcommand=scroll_x.set)

Unlike for the Text widget, the scrollable region of the Canvas is not updated automatically when its content is modified, so we need to define it and update it manually using the scrollregion argument:


canvas.bbox("all") returns the coordinates of the rectangle fitting the whole canvas content.