Skip to content
Advertisement

Is there a way to iterate through certain types of objects on a canvas in python tkinter?

For example, I’ve got a bunch of text objects of varying font families on a canvas, can I make some kind of call to iterate through these text objects and alter them?

Advertisement

Answer

You can get a list of all items on a canvas with the find_all() method and then just list them:

def get_canvas_items(canvas):
    item_list = canvas.find_all()
    for item in item_list:
        item_type = canvas.type(item)   # e.g. "text", "line", etc.
        item_keys = canvas.itemconfig(item).keys()  # item options
        # Do stuff...

Have a look at effbot: The Tkinter Canvas Widget for additional info on canvas.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement