Modeled after the scrolled canvas class from Grayons's Tkinter book. Used as the default canvas, which pops up automatically when using turtle graphics functions or the Turtle class.
| 226 | |
| 227 | |
| 228 | class ScrolledCanvas(TK.Frame): |
| 229 | """Modeled after the scrolled canvas class from Grayons's Tkinter book. |
| 230 | |
| 231 | Used as the default canvas, which pops up automatically when |
| 232 | using turtle graphics functions or the Turtle class. |
| 233 | """ |
| 234 | def __init__(self, master, width=500, height=350, |
| 235 | canvwidth=600, canvheight=500): |
| 236 | TK.Frame.__init__(self, master, width=width, height=height) |
| 237 | self._rootwindow = self.winfo_toplevel() |
| 238 | self.width, self.height = width, height |
| 239 | self.canvwidth, self.canvheight = canvwidth, canvheight |
| 240 | self.bg = "white" |
| 241 | self._canvas = TK.Canvas(master, width=width, height=height, |
| 242 | bg=self.bg, relief=TK.SUNKEN, borderwidth=2) |
| 243 | self.hscroll = TK.Scrollbar(master, command=self._canvas.xview, |
| 244 | orient=TK.HORIZONTAL) |
| 245 | self.vscroll = TK.Scrollbar(master, command=self._canvas.yview) |
| 246 | self._canvas.configure(xscrollcommand=self.hscroll.set, |
| 247 | yscrollcommand=self.vscroll.set) |
| 248 | self.rowconfigure(0, weight=1, minsize=0) |
| 249 | self.columnconfigure(0, weight=1, minsize=0) |
| 250 | self._canvas.grid(padx=1, in_ = self, pady=1, row=0, |
| 251 | column=0, rowspan=1, columnspan=1, sticky='news') |
| 252 | self.vscroll.grid(padx=1, in_ = self, pady=1, row=0, |
| 253 | column=1, rowspan=1, columnspan=1, sticky='news') |
| 254 | self.hscroll.grid(padx=1, in_ = self, pady=1, row=1, |
| 255 | column=0, rowspan=1, columnspan=1, sticky='news') |
| 256 | self.reset() |
| 257 | self._rootwindow.bind('<Configure>', self.onResize) |
| 258 | |
| 259 | def reset(self, canvwidth=None, canvheight=None, bg = None): |
| 260 | """Adjust canvas and scrollbars according to given canvas size.""" |
| 261 | if canvwidth: |
| 262 | self.canvwidth = canvwidth |
| 263 | if canvheight: |
| 264 | self.canvheight = canvheight |
| 265 | if bg: |
| 266 | self.bg = bg |
| 267 | self._canvas.config(bg=bg, |
| 268 | scrollregion=(-self.canvwidth//2, -self.canvheight//2, |
| 269 | self.canvwidth//2, self.canvheight//2)) |
| 270 | self._canvas.xview_moveto(0.5*(self.canvwidth - self.width + 30) / |
| 271 | self.canvwidth) |
| 272 | self._canvas.yview_moveto(0.5*(self.canvheight- self.height + 30) / |
| 273 | self.canvheight) |
| 274 | self.adjustScrolls() |
| 275 | |
| 276 | |
| 277 | def adjustScrolls(self): |
| 278 | """ Adjust scrollbars according to window- and canvas-size. |
| 279 | """ |
| 280 | cwidth = self._canvas.winfo_width() |
| 281 | cheight = self._canvas.winfo_height() |
| 282 | self._canvas.xview_moveto(0.5*(self.canvwidth-cwidth)/self.canvwidth) |
| 283 | self._canvas.yview_moveto(0.5*(self.canvheight-cheight)/self.canvheight) |
| 284 | if cwidth < self.canvwidth or cheight < self.canvheight: |
| 285 | self.hscroll.grid(padx=1, in_ = self, pady=1, row=1, |