Here is a code fragment showing the creation of a canvas
with horizontal and vertical scrollbars. In this
fragment, self is assumed to be a Frame widget.
self.canv = tk.Canvas(self, width=600, height=400,
scrollregion=(0, 0, 1200, 800))
self.canv.grid(row=0, column=0)
self.scrollY = tk.Scrollbar(self, orient=tk.VERTICAL,
command=self.canv.yview)
self.scrollY.grid(row=0, column=1, sticky=tk.N+tk.S)
self.scrollX = tk.Scrollbar(self, orient=tk.HORIZONTAL,
command=self.canv.xview)
self.scrollX.grid(row=1, column=0, sticky=tk.E+tk.W)
self.canv['xscrollcommand'] = self.scrollX.set
self.canv['yscrollcommand'] = self.scrollY.set
Notes:
The connection goes both ways. The canvas's xscrollcommand option has to be connected to
the horizontal scrollbar's .set
method, and the scrollbar's command
option has to be connected to the canvas's .xview method. The vertical scrollbar and
canvas must have the same mutual connection.
The sticky options on the .grid() method
calls for the scrollbars force them to stretch just
enough to fit the corresponding dimension of the
canvas.