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 = Canvas ( self, width=600, height=400, scrollregion=(0, 0, 1200, 800) ) self.canv.grid ( row=0, column=0 ) self.scrollY = Scrollbar ( self, orient=VERTICAL, command=self.canv.yview ) self.scrollY.grid ( row=0, column=1, sticky=N+S ) self.scrollX = Scrollbar ( self, orient=HORIZONTAL, command=self.canv.xview ) self.scrollX.grid ( row=1, column=0, sticky=E+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.