您必须创建两个Scrollbar
s 并将它们设置为画布的xscrollcommand
和yscrollcommand
选项。然后,当您配置scrollregion
最大宽度和高度时,如果需要,scollbars 将增长。
如果你Canvas
用这个非常基本的实现替换你的代码,你会看到结果:
from Tkinter import *
class ScrollableCanvas(Canvas):
def __init__(self, master, *args, **options):
self.frame = Frame(master)
Canvas.__init__(self, self.frame, *args, **options)
self.xscrollbar = Scrollbar(self.frame, command=self.xview, orient=HORIZONTAL)
self.yscrollbar = Scrollbar(self.frame, command=self.yview)
self.config(xscrollcommand=self.xscrollbar.set, yscrollcommand=self.yscrollbar.set)
def create_line(self, *args, **options):
Canvas.create_line(self, args, **options)
self._resize(*args)
def _resize(self, *args):
maxwidth = max(args[0], args[2], int(self['width']))
maxheight = max(args[1], args[3], int(self['height']))
self.config(scrollregion=(0, 0, maxwidth, maxheight))
def grid(self, *args, **options):
self.frame.grid(*args, **options)
Canvas.grid(self, row=0, column=0)
self.xscrollbar.grid(row=1, column=0, sticky=E+W)
self.yscrollbar.grid(row=0, column=1, sticky=N+S)