Python 如何关闭tkinter中的子窗口?
我只是在学习如何在tkinter中编写简单的GUI应用程序,当我想关闭child1作为child2函数的第一步时,我陷入了困境 我想要实现的是点击“下一步按钮”,上一个窗口关闭,新窗口出现。这里是我的python3代码的一个示例。在互联网上浏览顶级信息,但没有任何运气 很抱歉问了个愚蠢的问题,但没有其他人可以问:(Python 如何关闭tkinter中的子窗口?,python,tkinter,Python,Tkinter,我只是在学习如何在tkinter中编写简单的GUI应用程序,当我想关闭child1作为child2函数的第一步时,我陷入了困境 我想要实现的是点击“下一步按钮”,上一个窗口关闭,新窗口出现。这里是我的python3代码的一个示例。在互联网上浏览顶级信息,但没有任何运气 很抱歉问了个愚蠢的问题,但没有其他人可以问:( 您可以使用self.newWindow.destroy()关闭第一个窗口。参考Nazar Khan的答案,您需要将其声明为self的对象: self.newWindow=Toplev
您可以使用
self.newWindow.destroy()
关闭第一个窗口。参考Nazar Khan的答案,您需要将其声明为self的对象:
self.newWindow=Toplevel(self.master)
然后,您可以使用self.newWindow.destroy()关闭它。多亏了作者,我发现了一个错误: newWindow的命名应命名为 自我 就其功能而言
from tkinter import *
class TestApp:
def __init__(self, master):
self.master = master
master.minsize(width=800, height=640)
self.button = Button(master, text='Next', command=self.firstWindow)
self.button.pack()
def firstWindow(self):
self.master.withdraw()
self.newfirstWindow = Toplevel(self.master)
self.newfirstWindow.minsize(width=800, height=640)
self.button = Button(self.newfirstWindow, text='Next', command=self.secondWindow)
self.button.pack()
def secondWindow(self):
self.newfirstWindow.destroy()
self.newsecondWindow = Toplevel(self.master)
self.newsecondWindow.minsize(width=800, height=640)
self.button = Button(self.newsecondWindow, text='Quit', command=self.master.quit())
self.button.pack()
if __name__ == "__main__":
master = Tk()
App = TestApp(master)
master.mainloop()
self.newWindow.destroy()AttributeError:'TestApp'对象没有属性'newWindow'会产生错误我在这里发布之前尝试了这两种方法。始终返回错误“self.newWindow.destroy()AttributeError:'TestApp'对象没有属性'newWindow'”
from tkinter import *
class TestApp:
def __init__(self, master):
self.master = master
master.minsize(width=800, height=640)
self.button = Button(master, text='Next', command=self.firstWindow)
self.button.pack()
def firstWindow(self):
self.master.withdraw()
self.newfirstWindow = Toplevel(self.master)
self.newfirstWindow.minsize(width=800, height=640)
self.button = Button(self.newfirstWindow, text='Next', command=self.secondWindow)
self.button.pack()
def secondWindow(self):
self.newfirstWindow.destroy()
self.newsecondWindow = Toplevel(self.master)
self.newsecondWindow.minsize(width=800, height=640)
self.button = Button(self.newsecondWindow, text='Quit', command=self.master.quit())
self.button.pack()
if __name__ == "__main__":
master = Tk()
App = TestApp(master)
master.mainloop()