I’m tinkering around with tkinter, and have used it to make some dynamic graphics based on a dataset.
I started of with an arbitrary sized canvas of 1000×1000 pixels, and now I have my images made, I wondered if there is was anyway to crop the canvas around the unoccupied parts of the edges.
I think of a couple of ways to achieve this, one would be to trim each edge until an object is hit, however, I think this wouldn’t work because as far as I can tell the objects are directly addressed onto the canvas location, so any changes to the canvas in the top RHS would just result in objects moving in concert, another would be to (somehow) group all the objects into a single named object, get the borders and a somehow redraw the lot on a newly sized canvas.
I wondered if anyone had any ideas or done this before?
The bbox method of the canvas gives you the bounding box (opposite corners) of an object or objects on the canvas. So,
my_canvas.bbox("all")will return you a rectangle that encompasses all of the items on the canvas. According to the official Tk documentation, this method “may overestimate the actual bounding box by a few pixels”I’m not sure exactly what you mean by “crop” in the context of this question, but since you know the x/y of the upper left corner of the objects, you can use the move method to move all objects by -x1/-y1 pixels to move everything to the upper left corner.