Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 关闭tkinter中的窗口时出现问题_Python_Python 3.x_Tkinter - Fatal编程技术网

Python 关闭tkinter中的窗口时出现问题

Python 关闭tkinter中的窗口时出现问题,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,为了尽可能简短,在我的程序中,我从第1页开始,当我按下一个按钮想打开第2页并关闭第1页时,我设法打开了第2页,但我无法关闭第1页,我尝试使用.destroy(),但它关闭了所有内容,而不仅仅是页面。我在这里查看了一些问题,但是在与我的代码相同的布局中找不到太多问题,所以我不知道如何将其应用到我的代码中。这是我的第一个tkinter项目,所以我仍在努力 我的代码是 class Page1: def __init__(self,master): self.master = m

为了尽可能简短,在我的程序中,我从第1页开始,当我按下一个按钮想打开第2页并关闭第1页时,我设法打开了第2页,但我无法关闭第1页,我尝试使用
.destroy()
,但它关闭了所有内容,而不仅仅是页面。我在这里查看了一些问题,但是在与我的代码相同的布局中找不到太多问题,所以我不知道如何将其应用到我的代码中。这是我的第一个
tkinter
项目,所以我仍在努力

我的代码是

class Page1:
    def __init__(self,master):
        self.master = master
        #lots of labels and buttons
        self.BTNNextPage = ttk.Button(master, text = "Proceed",
                                  command = self.NextPage)
        self.BTNNextPage.place(x=450, y=420)

    def NextPage(self):
        self.newWindow = tk.Toplevel(self.master)
        self.app = Page2(self.newWindow)
        self.master.destroy()

class Page2():
    def __init__(self,master):
        self.master = master
        #tried Page1.destroy() here but Page1 has no attibute destroy
        #more labels and buttons

def main():
    widthpixels=690
    heightpixels=500
    root = tk.Tk()
    root.resizable(width=False, height=False)
    root.configure(background='black')
    root.iconbitmap("Image")
    root.wm_title("Title")
    root.geometry('{}x{}'.format(widthpixels, heightpixels))
    app = Page1(root)
    root.mainloop()

if __name__ == "__main__":
    main()

如果销毁
root
,它将销毁包含的所有小部件,包括
Page2
。要仅销毁第1页,一种可能性是使页面类继承自
tk.Frame
,以便它们具有
destroy
方法:

import tkinter as tk
from tkinter import ttk

class Page1(tk.Frame):
    def __init__(self, master, **kwargs):
        tk.Frame.__init__(self, master, **kwargs)
        self.pack(fill='both', expand=True) # display page 1
        #lots of labels and buttons:
        tk.Label(self, text='Page 1').place(relx=0.5, rely=0.5)
        self.BTNNextPage = ttk.Button(self, text="Proceed", command=self.NextPage)
        self.BTNNextPage.place(x=450, y=420)

    def NextPage(self):
        self.app = Page2(self.master) # create page 2
        self.destroy() # remove page 1

class Page2(tk.Frame):
    def __init__(self, master, **kwargs):
        tk.Frame.__init__(self, master, **kwargs)
        self.pack(fill='both', expand=True) # display page 2
        # create widgets on page 2
        tk.Label(self, text='Page 2').pack()
        tk.Button(self, text='Quit', command=self.master.destroy).pack(side='bottom')

def main():
    widthpixels=690
    heightpixels=500
    root = tk.Tk()
    root.resizable(width=False, height=False)
    root.configure(background='black')
    root.wm_title("Title")
    root.geometry('{}x{}'.format(widthpixels, heightpixels))
    app = Page1(root)
    root.mainloop()

if __name__ == "__main__":
    main()

如果销毁
root
,它将销毁包含的所有小部件,包括
Page2
。要仅销毁第1页,一种可能性是使页面类继承自
tk.Frame
,以便它们具有
destroy
方法:

import tkinter as tk
from tkinter import ttk

class Page1(tk.Frame):
    def __init__(self, master, **kwargs):
        tk.Frame.__init__(self, master, **kwargs)
        self.pack(fill='both', expand=True) # display page 1
        #lots of labels and buttons:
        tk.Label(self, text='Page 1').place(relx=0.5, rely=0.5)
        self.BTNNextPage = ttk.Button(self, text="Proceed", command=self.NextPage)
        self.BTNNextPage.place(x=450, y=420)

    def NextPage(self):
        self.app = Page2(self.master) # create page 2
        self.destroy() # remove page 1

class Page2(tk.Frame):
    def __init__(self, master, **kwargs):
        tk.Frame.__init__(self, master, **kwargs)
        self.pack(fill='both', expand=True) # display page 2
        # create widgets on page 2
        tk.Label(self, text='Page 2').pack()
        tk.Button(self, text='Quit', command=self.master.destroy).pack(side='bottom')

def main():
    widthpixels=690
    heightpixels=500
    root = tk.Tk()
    root.resizable(width=False, height=False)
    root.configure(background='black')
    root.wm_title("Title")
    root.geometry('{}x{}'.format(widthpixels, heightpixels))
    app = Page1(root)
    root.mainloop()

if __name__ == "__main__":
    main()

谢谢你的回答,但是page1没有被删除,page2只是叠加在它上面
self.destroy()
in
NextPage
会破坏page1,所以page2不仅仅是叠加在它上面。当我运行此操作时,当我按下下一个按钮时,page1和page2的内容只会同时出现在同一个窗口中我不明白为什么,我在我的电脑上试过了,它成功了:第一页出现了,然后当我点击“继续”时,第一页消失了,第二页替换了它。在
NextPage
中,尝试将
self.destroy()
放在
Page2(self.master)
之前。你的答案仍然是一样的,但是page1没有被删除,Page2只是覆盖在它上面
self.destroy()
中的
NextPage
会销毁page1,当我运行这个程序时,当我按下下一个按钮时,第1页和第2页的内容就会同时出现在同一个窗口中。我不明白为什么,我在我的电脑上尝试了一下,结果成功了:第1页出现了,然后当我点击“继续”时,第1页消失了,第2页替换了它。尝试将
self.destroy()