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.
| 325 | |
| 326 | |
| 327 | class ScrolledCanvas(TK.Frame): |
| 328 | """Modeled after the scrolled canvas class from Grayons's Tkinter book. |
| 329 | |
| 330 | Used as the default canvas, which pops up automatically when |
| 331 | using turtle graphics functions or the Turtle class. |
| 332 | """ |
| 333 | def __init__(self, master, width=500, height=350, |
| 334 | canvwidth=600, canvheight=500): |
| 335 | TK.Frame.__init__(self, master, width=width, height=height) |
| 336 | self._rootwindow = self.winfo_toplevel() |
| 337 | self.width, self.height = width, height |
| 338 | self.canvwidth, self.canvheight = canvwidth, canvheight |
| 339 | self.bg = "white" |
| 340 | self._canvas = TK.Canvas(master, width=width, height=height, |
| 341 | bg=self.bg, relief=TK.SUNKEN, borderwidth=2) |
| 342 | self.hscroll = TK.Scrollbar(master, command=self._canvas.xview, |
| 343 | orient=TK.HORIZONTAL) |
| 344 | self.vscroll = TK.Scrollbar(master, command=self._canvas.yview) |
| 345 | self._canvas.configure(xscrollcommand=self.hscroll.set, |
| 346 | yscrollcommand=self.vscroll.set) |
| 347 | self.rowconfigure(0, weight=1, minsize=0) |
| 348 | self.columnconfigure(0, weight=1, minsize=0) |
| 349 | self._canvas.grid(padx=1, in_ = self, pady=1, row=0, |
| 350 | column=0, rowspan=1, columnspan=1, sticky='news') |
| 351 | self.vscroll.grid(padx=1, in_ = self, pady=1, row=0, |
| 352 | column=1, rowspan=1, columnspan=1, sticky='news') |
| 353 | self.hscroll.grid(padx=1, in_ = self, pady=1, row=1, |
| 354 | column=0, rowspan=1, columnspan=1, sticky='news') |
| 355 | self.reset() |
| 356 | self._rootwindow.bind('<Configure>', self.onResize) |
| 357 | |
| 358 | def reset(self, canvwidth=None, canvheight=None, bg = None): |
| 359 | """Adjust canvas and scrollbars according to given canvas size.""" |
| 360 | if canvwidth: |
| 361 | self.canvwidth = canvwidth |
| 362 | if canvheight: |
| 363 | self.canvheight = canvheight |
| 364 | if bg: |
| 365 | self.bg = bg |
| 366 | self._canvas.config(bg=bg, |
| 367 | scrollregion=(-self.canvwidth//2, -self.canvheight//2, |
| 368 | self.canvwidth//2, self.canvheight//2)) |
| 369 | self._canvas.xview_moveto(0.5*(self.canvwidth - self.width + 30) / |
| 370 | self.canvwidth) |
| 371 | self._canvas.yview_moveto(0.5*(self.canvheight- self.height + 30) / |
| 372 | self.canvheight) |
| 373 | self.adjustScrolls() |
| 374 | |
| 375 | |
| 376 | def adjustScrolls(self): |
| 377 | """ Adjust scrollbars according to window- and canvas-size. |
| 378 | """ |
| 379 | cwidth = self._canvas.winfo_width() |
| 380 | cheight = self._canvas.winfo_height() |
| 381 | self._canvas.xview_moveto(0.5*(self.canvwidth-cwidth)/self.canvwidth) |
| 382 | self._canvas.yview_moveto(0.5*(self.canvheight-cheight)/self.canvheight) |
| 383 | if cwidth < self.canvwidth or cheight < self.canvheight: |
| 384 | self.hscroll.grid(padx=1, in_ = self, pady=1, row=1, |
no outgoing calls
no test coverage detected
searching dependent graphs…