1

How do you make a button open a duplicate window of the main page in a new window?

What would the python code be to do this with Tkinter?

For clarification, in notepad++ when you right click on the tab that you are in, it shows you a whole menu.

Then, there is a dropdown menu with an option that says, open in a new instance or move to a new instance.

This would then open the tab you selected into a new tab/window.

I haven't tried anything yet but I did look up about how to do this type of thing and there were no results on what exactly I wanted to do.

Can you try to show me an example of how what I need to do can be done?

Code:

Found Here

1 Answer 1

2

The simplest solution is to make your window be a subclass of Toplevel. Then, each time you need a new window, create a new instance of the class.

You can then leave the root window blank and hide it so that you only see your custom windows. It's important to make sure that you destroy the root window after the user has deleted the last application window, or else you'll end up with an invisible window that will be hard to kill.

Here is a bit of a contrived example:

import tkinter as tk

class AppWindow(tk.Toplevel):
    def __init__(self, root):
        tk.Toplevel.__init__(self, root)

        self.root = root
        menubar = tk.Menu(self)
        windowMenu = tk.Menu(menubar)
        windowMenu.add_command(label="New Window", command=self.new_window)
        windowMenu.add_command(label="Quit", command=root.destroy)

        menubar.add_cascade(label="Window", menu=windowMenu)
        self.configure(menu=menubar)

        self.text = tk.Text(self)
        self.vsb = tk.Scrollbar(self, orient="vertical", command=self.text.yview)
        self.text.configure(yscrollcommand=self.vsb.set)

        self.vsb.pack(side="right", fill="y")
        self.text.pack(side="left", fill="both", expand=True)

        # call a function to destroy the root window when the last
        # instance of AppWindow is destroyed
        self.wm_protocol("WM_DELETE_WINDOW", self.exit_on_last_window)

    def new_window(self):
        AppWindow(self.root)

    def exit_on_last_window(self):
        """Destroy the root window when no other windows exist"""
        self.destroy()
        if not any([window.winfo_exists() for window in self.root.winfo_children()]):
            self.root.destroy()

    def quit(self):
        self.root.destroy()

# create the root, then hide it. The app will create its
# own windows as Toplevels
root = tk.Tk()
root.withdraw()

# create the first window, then start the event loop
AppWindow(root)
tk.mainloop()
Sign up to request clarification or add additional context in comments.

1 Comment

this works with me, however, How does it fit in with the code I just added to my new edit? I want the whole code for the page to be duplicated when I hit the new file button

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.