按钮在pycharm中使用python和tkinter在新窗口中打开主页的重复窗口

时间:2019-04-03 22:16:19

标签: python tkinter

如何使按钮在新窗口中打开主页的重复窗口?

使用Tkinter进行此操作的python代码是什么?

为澄清起见,在notepad ++中,当您右键单击所位于的选项卡时,它将显示整个菜单。

然后,有一个下拉菜单,其中包含一个选项,指出在新实例中打开或移至新实例。

然后将您选择的选项卡打开到新的选项卡/窗口中。

我还没有尝试过任何东西,但是我确实检查了如何做这种事情,但是我想做的事情没有任何结果。

您可以尝试向我展示如何完成我需要做的事吗?

代码:

Found Here

1 个答案:

答案 0 :(得分:1)

最简单的解决方案是使窗口成为Toplevel的子类。然后,每次需要一个新窗口时,请创建该类的新实例。

然后可以将根窗口保留为空白并将其隐藏,以便仅看到自定义窗口。重要的是要确保在用户删除了最后一个应用程序窗口后销毁根窗口,否则最终将得到一个不可见的不可见窗口。

这是一个人为的例子:

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()
相关问题