tkintertkinter-canvastkinter-layout

grid_slaves equivalent in Canvas in tkinter


I'm trying to access an item in Canvas in tkinter, like I can in a Frame using grid_slaves()

Let's say for example I want to access a subwidget:

subwidget = Frame.grid_slaves(row=0, column=0)[0]

I can't use grid_slaves() on a Canvas, and I was wondering what the equivalent would be for something like:

object = Canvas.grid_slaves(row=0, column=0)[0]

Solution

  • You can use the canvas method find_all along with the canvas methods type and itemcget to get the information you need.

    for item in canvas.find_all():
        if canvas.type(item) == "window":
            print(f"item id: {item} widget: {canvas.itemcget(item, 'window')}")
    

    If you give all of the items you want to find a unique tag, you can use find_withtag to return only the items with that tag.