Next / Previous / Contents

22.2. Connecting a Scrollbar to another widget

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: