Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stacking Order Maintenance in Tkinter

Tags:

tkinter

Is there a way to keep consistent stacking order of widgets (particularly images) from within Tkinter? For example, I may have two rectangles, two triangles, and a circle all at the same location on the canvas. The circle moves to wherever the mouse was last clicked, but I always want it to be drawn on top of rectangles and behind triangles. The default behavior is of course to place the last-drawn object at a given location on top.

I've only found the lift() and lower() methods to address stacking in Tkinter thus far. I could theoretically make some messy checks at every re-draw to see whether triangles exist at the destination and do some lift()/lower() for each shape there until things are the way I want, but it seems an absurd solution. I expected there to be a sort of "layer" variable on canvas objects so it would passively remember that items of layer 3 should always be behind those of layer 1.

I apologize if there is a simple method to solve my problem, but as of yet all my searching has been to no avail. I will be thankful for any input!

like image 307
Taugosz Avatar asked Feb 21 '23 06:02

Taugosz


1 Answers

Tag all triangles with "triangle", all rectangles with "rectangle" and all circles with "circle". You can then use the lift or lower method to raise or lower all triangles, circles or rectangles at once. It takes just a couple of statements and will happen in less than the blink of an eye even if you have thousands of items.

Since you asked about a built-in layer mechanism: tk has no such thing but it's really easy to add, all the tools are there. So, instead of sprinkling lift or lower commands everywhere, have all your drawing go through a special command that handles the layering for you.

For example, here is a an example of a command that accepts a layer number and makes sure everything it draws is on that layer:

def add_to_layer(self, layer, command, coords, **kwargs):
    layer_tag = "layer %s" % layer
    if layer_tag not in self._layers: self._layers.append(layer_tag)
    tags = kwargs.setdefault("tags", [])
    tags.append(layer_tag)
    item_id = command(coords, **kwargs)
    self._adjust_layers()
    return item_id

def _adjust_layers(self):
    for layer in sorted(self._layers):
        self.canvas.lift(layer)

You would then call it like this:

# draw a circle on layer 1:
self.add_to_layer(1, self.canvas.create_oval, (x0,y0,x1,y1), outline="red")
# draw a square on layer 2:
self.add_to_layer(2, self.canvas.create_rectangle, (x0,y0,x1,y1), fill="blue")

There are many other ways to accomplish the same thing, the above is just a quick and dirty example off the top of my head (and not guaranteed to be 100% correct, but I think it illustrates the point).

The basic idea is that all object creation goes through a single function which can adjust the stacking order each time an item is created. That way you only have to add those lift/lower commands in one place.

like image 61
Bryan Oakley Avatar answered May 19 '23 17:05

Bryan Oakley