MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / ScrolledCanvas

Class ScrolledCanvas

tools/python-3.11.9-amd64/Lib/turtle.py:331–428  ·  view source on GitHub ↗

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.

Source from the content-addressed store, hash-verified

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

Callers 1

setupcanvasMethod · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected