Python 如何防止tkinter中出现多个窗口?

Python 如何防止tkinter中出现多个窗口?,python,user-interface,tkinter,Python,User Interface,Tkinter,当我运行它时,loginpage和GUI窗口都会打开。如果我错了,请纠正我,但我认为问题可能出在 import tkinter as tk from tkinter import * from tkinter import ttk LARGE_FONT = ("Verdana", 12) class pages(tk.Tk): #starts us off in the login page def __init__(self, *args, **kwarg

当我运行它时,loginpage和GUI窗口都会打开。如果我错了,请纠正我,但我认为问题可能出在

import tkinter as tk
from tkinter import *
from tkinter import ttk

LARGE_FONT = ("Verdana", 12)
class pages(tk.Tk):
    #starts us off in the login page
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        tk.Tk.wm_title(self, "ScanNET")
        tk.Tk.wm_minsize(self, 800, 800)

        container = tk.Frame(self)

        container.pack(side=TOP, fill=BOTH, expand=True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (loginpage, GUI):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky=N+E+S+W)

        self.show_frame(loginpage)
        
    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()
    
class loginpage(tk.Frame):
    #login page content
    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        loginlabel = tk.Label(self, text="login page", font=LARGE_FONT)
        loginlabel.pack(padx=10, pady=10)

        #button moves you to gui
        loginbutton1 = tk.Button(self, text= "Go to GUI", command=lambda: controller.show_frame(GUI))
        loginbutton1.pack()

class GUI(tk.Frame):
    def __init__(self, parent, controller):
        #all widths and heights aren't official, most likely change
        tk.Frame.__init__(self, parent)
        self.root = tk.Tk()

        #the tabs
        my_notebook = ttk.Notebook(self.root)
        my_notebook.pack()
        devicestab = Frame(my_notebook, width=800, height=600)
        reportstab = Frame(my_notebook, width=800, height=600)
        devicestab.pack(fill=BOTH, expand=1)
        reportstab.pack(fill=BOTH, expand=1)
        my_notebook.add(devicestab, text="Devices")
        my_notebook.add(reportstab, text="Reports")

        #contents for devices tab
        devicesleft = LabelFrame(devicestab, text="Devices found: ", padx=5, pady=5, width=500, height=600)
        devicesleft.grid(row=0, column=0)
        devicesright = LabelFrame(devicestab, text="Activity Feed: ", padx=5, pady=5, width=300 , height=600)
        devicesright.grid(row=0, column=1)

        #contents for reports tab
        reportsleft = LabelFrame(reportstab, text="Report Summaries: ", padx=5, pady=5, width=400 , height=600)
        reportsleft.grid(row=0, column=0)
        reportsright= LabelFrame(reportstab, text="Charts and Diagrams: ", padx=5, pady=5, width=400 , height=600)
        reportsright.grid(row=0, column=1)


app = pages()
app.mainloop()

参与GUI类。我到处都搜索过了,但似乎找不到一种方法,可以将第一页作为登录页,然后将其移动到第二页,该页上有使用笔记本的选项卡。我觉得好像在
ttk.Notebook()
部分中必须有其他内容,然后可能删除
self.root=tk.tk()
。我很想听听你们的想法。

我假设您希望笔记本与其他小部件位于同一个小部件中,因此您不应该使用
tk.tk()
,然后将笔记本放在
parent
中,它已经是您的
root
。检查我答案末尾的代码。另外,由于您的代码有很多问题,我做了一些更改和注释,这将帮助您在tkinter中编写更好的代码。请仔细阅读。您可能还想学习该网页

 tk.Frame.__init__(self, parent)
 self.root = tk.Tk()
 my_notebook = ttk.Notebook(self.root)

my_notebook
的父项应该是
self
。一定要配合@acw1668的注释。我很感谢您的更改和解释它们的评论,我对python和tkinter还不熟悉。非常感谢你的帮助
import tkinter as tk
# from tkinter import * # just don't do this
from tkinter import ttk

LARGE_FONT = ("Verdana", 12)
# class pages(tk.Tk):
class Pages(tk.Tk): # class names should start with upper case
    #starts us off in the login page
    # def __init__(self, *args, **kwargs):
    def __init__(self):
        # tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.__init__(self)

        # tk.Tk.wm_title(self, "ScanNET")
        self.winfo_toplevel().title("ScanNET")
        # tk.Tk.wm_minsize(self, 800, 800)
        self.wm_minsize(800, 800) # since you defined tk.Tk as pages parent you can call Tk methods directly

        container = tk.Frame(self)

        # container.pack(side=TOP, fill=BOTH, expand=True)

        # container.grid_rowconfigure(0, weight=1)
        # container.grid_columnconfigure(0, weight=1)
        container.grid(row=0, column = 0) # don't use pack if you want to use grid

        self.frames = {}

        for F in (loginpage, GUI):
            frame = F(container, self)
            self.frames[F] = frame
            # frame.grid(row=0, column=0, sticky=N+E+S+W)
            frame.grid(row=0, column=0, sticky='NESW') #since we are not importing all we are not importing tk.W but you can use string instead

        self.show_frame(loginpage)
        
    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()
    
class loginpage(tk.Frame):
    #login page content
    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        loginlabel = tk.Label(self, text="login page", font=LARGE_FONT)
        loginlabel.pack(padx=10, pady=10)

        #button moves you to gui
        loginbutton1 = tk.Button(self, text= "Go to GUI", command=lambda: controller.show_frame(GUI))
        loginbutton1.pack()

class GUI(tk.Frame):
    def __init__(self, parent, controller):
        #all widths and heights aren't official, most likely change
        tk.Frame.__init__(self, parent)
        # self.root = tk.Tk() # don't create new Tk objects, you just need one. The others should be Toplevel objects
        ### self.root = tk.Toplevel() ### this would be the correct way of creating a new window but you don't want to do that here your root is your parent

        #the tabs
        # my_notebook = ttk.Notebook(self.root)
        my_notebook = ttk.Notebook(self) # this is how you place the notebook in the Frame widget and not in a new one
        # my_notebook.pack() 
        my_notebook.grid() # we are now using grid so it will not accept pack anymore
        # devicestab = Frame(my_notebook, width=800, height=600)
        devicestab = tk.Frame(my_notebook, width=800, height=600) # again, since we are not importing al we have to use tk. before tkinter methods
        # reportstab = Frame(my_notebook, width=800, height=600)
        reportstab = tk.Frame(my_notebook, width=800, height=600)
        # devicestab.pack(fill=BOTH, expand=1)
        devicestab.pack(fill="both", expand=1) # instead of tk.BOTH we can use "both"
        reportstab.pack(fill="both", expand=1)
        my_notebook.add(devicestab, text="Devices")
        my_notebook.add(reportstab, text="Reports")

        #contents for devices tab
        devicesleft = tk.LabelFrame(devicestab, text="Devices found: ", padx=5, pady=5, width=500, height=600)
        devicesleft.grid(row=0, column=0)
        devicesright = tk.LabelFrame(devicestab, text="Activity Feed: ", padx=5, pady=5, width=300 , height=600)
        devicesright.grid(row=0, column=1)

        #contents for reports tab
        reportsleft = tk.LabelFrame(reportstab, text="Report Summaries: ", padx=5, pady=5, width=400 , height=600)
        reportsleft.grid(row=0, column=0)
        reportsright= tk.LabelFrame(reportstab, text="Charts and Diagrams: ", padx=5, pady=5, width=400 , height=600)
        reportsright.grid(row=0, column=1)


app = Pages()
app.mainloop()