Search code examples
pythontkinterframetkinter-canvas

Finding child canvases in frame


Lets say I have a frame which has many widgets inside it, including canvases labels and buttons.

Can i use:

counter = 0
for Canvas in frame.winfo_children()
    counter = counter + 1
print(counter)

to find the the number of canvases in that frame?

Thanks for any help in advance


Solution

  • No, that won't work. You have to ask tkinter for the class of the widget:

    counter = 0
    for child in frame.winfo_children()
        widget_class = child.winfo_class()
        if widget_class == "Canvas":
            counter = counter + 1