Home > Blockchain >  Is it possible to open a new window while closing the current one?
Is it possible to open a new window while closing the current one?

Time:04-08

I want to open a new window while closing the current one.

class UserInterface:          
def __init__(self, master, username):      
    self.master = master
    self.master.title('Kill Smoking')
    self.master.geometry("500x500")
    labelInterfaceTitle = Label(self.master, text = "Welcome to Kill Smoking")
    labelInterfaceTitle.grid(row = 0, column = 2, padx = 10, pady = 10)
    Planbtn = Button(self.master, text = "Access Smoking plan", command = self.AccessPlan)
    Help_pagesbtn = Button(self.master, text = "Support pages")
   
    self.progress_username = username
    

    Planbtn.grid(row = 10, column = 10)
    Help_pagesbtn.grid(row = 12, column = 10)

Here is my function to open a new window, however what would I need to do to close the current one?

def AccessPlan(self):
    pass_progress_username = self.progress_username
    self.AccessPlan = Toplevel(self.master)
    self.app = SmokingPlan(self.AccessPlan, pass_progress_username)
    

CodePudding user response:

You can use withdraw() like the following example.

import tkinter as tk


def new_window():
    new_root = tk.Toplevel()
    new_root.geometry("500x500")
    root.withdraw()

    
root = tk.Tk()
root.geometry("500x500")
button = tk.Button(root, text="new window", command=lambda: new_window())
button.pack()


    
root.mainloop()

CodePudding user response:

I recommend not destroying the root window. Tkinter is designed to always have a root window. Instead, delete the contents of the root window and reuse it.

def AccessPlan(self):
    ...
    for child in self.master.winfo_children():
        child.destroy()
    self.app = SmokingPlan(self.master, pass_progress_username)
    
  • Related