简体   繁体   中英

Close main window after opening a new one

I found this example of code here on stackoverflow and I would like to make the first window close when a new one is opened. So what I would like is when a new window is opened, the main one should be closed automatically.

#!/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()

You would withdraw the main window, but you have no way to close the program after the button click in the Toplevel, when the main window is still open but doesn't show Also pick one or the other of (but don't use both)

import Tkinter as tk

from Tkinter import *

This opens a 2nd Toplevel which allows you to exit the program

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()

In your code:

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

You are not creating a new window independent completely from your root (or master ) but rather a child of the Toplevel ( master in your case), of course this new child toplevel will act independent of the master until the master gets detroyed where the child toplevel will be destroyed as well,

To make it completely seperate, create a new instance of the Tk object and have it close the windowclass window (destroy its object):

self.newWindow = Tk()

you have two options here:

1 - Either you need to specify in the windowclass1.close_window() , that you want to destroy the cls object when you create the windowclass1() object, this way:

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

2 - Which is the preferred one for generality, is to destroy the cls after you create windowclass1 object in the windowclass.command() method, like this:

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

and make the quitButton in the __init__() of windowclass1 like this:

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

to quit completely your program

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM