0

What I want is when I click on a button a new window should get open and that window should be a child of the same main window, But what I am getting is a new Instance of a new window. How do I solve this in tkinter?

Here is the screenshot of what I do not want, Every time a new instance of window is getting is created, I want to make a child instance of same main window:

def login_success():
def c1():
    top = Toplevel()
    top.title("c1")
    top.geometry("1000x600")


def c2():
    top = Toplevel()
    top.title("c1")
    top.geometry("1000x600")


def c3():
    top = Toplevel()
    top.title("c2")
    top.geometry("1000x600")

def write_frames():
     top = Toplevel()
     top.title("t2")
     top.geometry("1000x600")
     b1 = Button(top, text="c1", command=c1)
     b1.pack()
     b2 = Button(top, text="c2", command=c2)
     b2.pack()
     b3 = Button(top, text="c3", command=c3)
     b3.pack()

def write_instructions():
    top = Toplevel()
    top.title("t1")
    top.geometry("1000x600")
root = Tk()
root.geometry("1000x600")
button1 = Button(root,
               text="Frames",
               command=write_frames)
button1.pack()
button2 = Button(root,
               text="Instructions",
               command=write_instructions)
button2.pack()
2
  • 3
    This question is too vague. Please show a minimal reproducible example of what you've tried. Commented Sep 29, 2019 at 4:41
  • @BryanOakley Ok I have done the changes, Please check it out, On left hand side as you can see every time a new instance is getting created, which I do not want, I want to open new window within same main window. Commented Sep 29, 2019 at 12:08

1 Answer 1

1

You just have to pass root as a parameter to every function if i understand correctly what you mean

def login_success():


    def c1(root):
            top = Toplevel(root)
            top.title("c1")
            top.geometry("1000x600")


    def c2(root):
        top = Toplevel(root)
        top.title("c1")
        top.geometry("1000x600")


    def c3(root):
        top = Toplevel(root)
        top.title("c2")
        top.geometry("1000x600")

    def write_frames(root):
         top = Toplevel(root)
         top.title("t2")
         top.geometry("1000x600")
         b1 = Button(top, text="c1", command=lambda: c1(root))
         b1.pack()
         b2 = Button(top, text="c2", command=lambda: c2(root))
         b2.pack()
         b3 = Button(top, text="c3", command=lambda: c3(root))
         b3.pack()

    def write_instructions(root):
        top = Toplevel(root)
        top.title("t1")
        top.geometry("1000x600")

    root = Tk()
    root.geometry("1000x600")
    button1 = Button(root,
                   text="Frames",
                   command=lambda: write_frames(root))
    button1.pack()
    button2 = Button(root,
                   text="Instructions",
                   command=lambda: write_instructions(root))
    button2.pack()
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much it helped a little, But still I have a question that a new instance is getting created, I just want that only parent window should be running and all other window's should be a child but a new instance is getting created. You can check my screenshot's left side every time a new process is getting created, which is getting repeated even after running your code. Should I use root.withdraw to close previous window's or is there any way where everything can be still done in the same instance of window.
So you want to create widgets inside this window? Root.withdraw will just hide the tk window.
Yes root.withdraw is just hiding window, I actually want to move from one frame to another but within same window. How do I do that. Every time a new window is getting open.
I cannot understand what you are trying to do. Frames are completely irrelevant. You probably want to add widgets(button, text etc) in this window.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.