打开新窗口后关闭主窗口

时间:2015-05-13 12:56:33

标签: python python-2.7 tkinter

我在stackoverflow上找到了这个代码示例,我想在打开一个新窗口时关闭第一个窗口。 所以我想要的是当一个新窗口打开时,主窗口应该自动关闭。

#!/usr/bin/env python
import Tkinter as tk

from Tkinter import *

class windowclass():

        def __init__(self,master):
                self.master = master
                self.frame = tk.Frame(master)
                self.lbl = Label(master , text = "Label")
                self.lbl.pack()
                self.btn = Button(master , text = "Button" , command = self.command )
                self.btn.pack()
                self.frame.pack()

        def command(self):
                print 'Button is pressed!'

                self.newWindow = tk.Toplevel(self.master)
                self.app = windowclass1(self.newWindow)

class windowclass1():

        def __init__(self , master):
                self.master = master
                self.frame = tk.Frame(master)
                master.title("a")
                self.quitButton = tk.Button(self.frame, text = 'Quit', width = 25 , command = self.close_window)
                self.quitButton.pack()
                self.frame.pack()


        def close_window(self):
                self.master.destroy()


root = Tk()

root.title("window")

root.geometry("350x50")

cls = windowclass(root)

root.mainloop()

2 个答案:

答案 0 :(得分:0)

你会撤回主窗口,但是当主窗口仍然打开但没有显示时,你无法在按钮点击Toplevel后关闭程序也选择其中一个(但不要)使用两者)

import Tkinter as tk

from Tkinter import *

这将打开第二个Toplevel,允许您退出程序

import Tkinter as tk

class windowclass():

        def __init__(self,master):
                self.master = master
                ##self.frame = tk.Frame(master)  not used
                self.lbl = tk.Label(master , text = "Label")
                self.lbl.pack()
                self.btn = tk.Button(master , text = "Button" , command = self.command )
                self.btn.pack()
                ##self.frame.pack()  not used

        def command(self):
                print 'Button is pressed!'

                self.master.withdraw()
                toplevel=tk.Toplevel(self.master)
                tk.Button(toplevel, text="Exit the program",
                          command=self.master.quit).pack()
                self.newWindow = tk.Toplevel(self.master)
                self.app = windowclass1(self.newWindow)

class windowclass1():

        def __init__(self , master):
                """ note that "master" here refers to the TopLevel
                """
                self.master = master
                self.frame = tk.Frame(master)
                master.title("a")
                self.quitButton = tk.Button(self.frame,
                                  text = 'Quit this TopLevel',
                                  width = 25 , command = self.close_window)
                self.quitButton.pack()
                self.frame.pack()

        def close_window(self):
                self.master.destroy()  ## closes this TopLevel only

root = tk.Tk()

root.title("window")

root.geometry("350x50")

cls = windowclass(root)
root.mainloop()

答案 1 :(得分:0)

在您的代码中:

self.newWindow = tk.Toplevel(self.master)

你没有完全独立于你的根(或master)创建一个新窗口,而是Toplevel的一个孩子(在你的情况下是master),当然这是新的child toplevel将独立于master行动,直到master被摧毁,child toplevel也将被销毁,

要使其完全独立,请创建Tk对象的新实例并使其关闭windowclass窗口(销毁其对象):

self.newWindow = Tk()

你有两个选择:

1 - 您需要在windowclass1.close_window()中指定在创建cls对象时想要销毁windowclass1()对象,这样:

def close_window(self):
                cls.master.destroy()

2 - 通用性的首选方法之一是在cls方法中创建windowclass1对象后销毁windowclass.command(),如下所示:

def command(self):
                print 'Button is pressed!'
                self.newWindow = Tk()
                self.app = windowclass1(self.newWindow)
                self.master.destroy()

并在windowclass1的__init__()中创建quitButton,如下所示:

 self.quitButton = tk.Button(self.frame, text = 'Quit', width = 25, command = self.master.quit) 

完全退出程序