0

How would I open the second window and close the previous one I was one. So with my code i press ok on the first window it brings me to the second one, but I want the first one to close. I've been struggling for the past 2 hours. thanks

from tkinter import *

class Welcome():

 def __init__(self,master):

    self.master= master
    self.master.geometry("1080x800+200+200")
    self.master.title("Sphere Booking and Check-in")

    self.label1=Label(self.master,text="Sphere Booking and Check-in",fg="black",font=("Helvetica",25)).grid(row=0,column=2)
    self.button1=Button(self.master,text="OK",fg="blue",command=self.gotoWages).grid(row=6,column=2)
    self.button2=Button(self.master,text="quit",fg="blue",command=self.finish).grid(row=6,column=3)

def finish(self):

    self.master.destroy()

def gotoWages(self):

    root2=Toplevel(self.master)
    myGUI=Wages(root2)

class Wages():

  def __init__(self,master):

    self.nhours= DoubleVar()
    self.salaryh= DoubleVar()

    self.master= master
    self.master.geometry("1080x800+200+200")
    self.master.title("Sphere Booking and Check-in")

    self.label1=Label(self.master,text="Sphere Booking and Check-in",fg="black",font=("Helvetica",25)).grid(row=0,column=2)
    self.label2=Label(self.master,text="enter your salary per hour").grid(row=3,column=0)
    self.label3=Label(self.master,text="enter the number of hours worked").grid(row=4,column=0)

    self.mysalary= Entry(self.master, textvariable= self.salaryh).grid(row=3, column=3)
    self.mysalary= Entry(self.master, textvariable= self.nhours).grid(row=4, column=3)
    self.button1=Button(self.master,text="OK",fg="blue").grid(row=5,column=3)
    self.button2=Button(self.master,text="quit",fg="blue",command=self.myquit).grid(row=6,column=3)

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

def main():

  root=Tk()
  myGUIWelcome=Welcome(root)
  root.mainloop()

if __name__ == '__main__':
  main()
ylimes
  • 77
  • 1
  • 3
  • 10

1 Answers1

0

As mention is the following post "Disable the underlying window when a popup is created in Python TKinter", You have just to add a call to withdraw() of your master windows.

def gotoWages(self):

    root2=Toplevel(self.master)
    self.master.withdraw() # windows becomes invisible
    myGUI=Wages(root2)

EDIT: Add the solution to go back to the main window.

To show the main window class Welcome() when closing the class Wages(), add the call to functions update() then deiconify() as follow:

Step1 - store the Welcome() handle into the Wages().

In the Welcome::gotoWages() function, add the extra parameter self.master.

myGUI=Wages(root2,self.master)

In the Wages::__init__() function, manage the extra parameter mainwnd and store it.

def __init__(self,master,mainwnd):

    self.nhours= DoubleVar()
    self.salaryh= DoubleVar()

    self.mainwnd = mainwnd # store the 'self.master` of the main window
    self.master= master

Step 2 - use the stored mainwnd to show the Welcome window

Modify the Wages::myquit() function:

def myquit(self):
    self.master.destroy() # close the current Wages window
    self.mainwnd.update() # update the main window
    self.mainwnd.deiconify() # un-minimize the main window
Community
  • 1
  • 1
J. Piquard
  • 1,665
  • 2
  • 12
  • 17