how to insert a widget to another, if they are from different classes. and how to create a universal function of closing the child widgets?
<p>this is my code. I can't put label2 to self.main, and I don't know, how to write a generic function code, that would close the child widgets, that can be specified in the arguments.</p>

<pre><code>import tkinter


class mainwin:
    def __init__(self):
        self.root = tkinter.Tk()
        self.main = tkinter.Canvas(self.root, width=200, height=400)
        self.main.place(x=0, y=0, relwidth=1, relheight=1)
        self.main.config(bg='green')
        self.root.mainloop()


class addlabel:
    def __init__(self):
        self.label2 = tkinter.Label(mainwin.main, height=2, width=50,   text='Hello Noob!!') 
        #can't put on the canvas 'main'
        self.label2.place(x=0, y=50)
        self.exit_button = tkinter.Button(self.label2, text='Exit')
        self.exit.button.bind('&lt;1&gt;', quit_from_widget)

'''
class quit_from_widget:

  def __init__(self):
    # what code should be written here, to quit any child widget.
'''

mainwin()
addlabel()
</code></pre>

<p>You might be able to use: </p>

<pre><code>mylist = parent.winfo_children();
</code></pre>

<p>Then use a for loop and destroy() to close them off</p>

<p>The main reason you can't put the label in is because you are calling mainloop() before addLabel. The program loops through the code and doesn't execute addlabel() until you close the mainwin() function.</p>

<p>secondly, you can't do mainw.main. the class has no reference to that function. instead try adding a parent function to your addlabel like so:</p>

<pre><code>class addlabel:
def __init__(self, parent):
    self.label2 = tkinter.Label(parent, height=2, width=50,   text='Hello Noob!!')
    self.label2.place(x=0, y=50)
    self.exit_button = tkinter.Button(self.label2, text='Exit')
    self.exit_button.bind('&lt;1&gt;', quit)
</code></pre>

<p>Then, when you call the function in mainw class (before the self.root.mainloop() line) you would write:</p>

<pre><code>addlabel(self.main)
</code></pre>

python class tkinter widget