Is it possible to keep the same window for every class in python's tkinter?
Question: I am trying to create a program in tkinter which allows me to open an initial
window then to keep it throughout all classes used. For example, if I was to
create a button in a window then when I click this button, it would exuecute a
method that destroys the widget, and then executes a new class that builds a
new screen within the same window, such as text opposed to a button.

    
    
    from tkinter import *
    
    class Window1:
    
        def __init__(self, master):
            self.master = master
            self.label = Button(self.master, text = "Example", command = self.load_new)
            self.label.pack()
    
        def load_new(self):
            self.label.destroy()
            ## Code to execute next class
    
    class Window2:
    
        def __init__(self, master):
            self.master = master
            self.label = Label(self.master, text = "Example")
            self.label.pack()
    
    def main():
        root = Tk()
        run = Window1(root)
        root.mainloop()
    
    if __name__ == '__main__':
    main()
    

I understand this is less practical, but I am curious. Cheers.


Answer: `Tk()` creates main window and variable `root` gives you access to this
window. You can use `root` as argument for `Window2` and you will have access
to main window inside `Window2`

    
    
    from tkinter import *
    
    
    class Window1:
    
        def __init__(self, master):
    
            # keep `root` in `self.master`
            self.master = master 
    
            self.label = Button(self.master, text="Example", command=self.load_new)
            self.label.pack()
    
        def load_new(self):
            self.label.destroy()
    
            # use `root` with another class
            self.another = Window2(self.master)
    
    
    class Window2:
    
        def __init__(self, master):
    
            # keep `root` in `self.master`
            self.master = master
    
            self.label = Label(self.master, text="Example")
            self.label.pack()
    
    
    root = Tk()
    run = Window1(root)
    root.mainloop()
    

\--

Probably nobody use another class to create `Label` in place of `Button` ;)

\--

**EDIT:** In this example using names `Window1` and `Windows2` is misleading
because there is only one window and two classes which use this window. I
would rather use names `FirstOwner`, `SecondOwner`

