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

Time:11-29

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?

CodePudding user response:

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.

  • Related