2017-02-26 161 views
0

如何打開第二個窗口並關閉前一個窗口。因此,在我的代碼中,按下第一個窗口中的第一個窗口,然後按第二個窗口中的第一個窗口,但我希望第一個窗口關閉。我一直在掙扎過去的2個小時。感謝使用Tkinter iwant打開第二個窗口,然後關閉前一個窗口

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

回答

0

如上面所提到的是下面的帖子"Disable the underlying window when a popup is created in Python TKinter",你只需一個電話添加到您的主窗口withdraw()

def gotoWages(self): 

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

編輯:添加解決回到主窗口。

要顯示在主窗口class Welcome()關閉class Wages()時,添加呼叫,然後deiconify()功能update()如下:

第一步 - 在Welcome()手柄存入Wages()

Welcome::gotoWages()函數中,添加額外的參數self.master

myGUI=Wages(root2,self.master) 

Wages::__init__()功能,管理額外的參數mainwnd和存儲。

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 

步驟2 - 使用存儲mainwnd展現Welcome窗口

修改Wages::myquit()功能:

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 
+0

非常感謝你,也感謝您的鏈接到其他發佈! – ylimes

+0

要再次顯示主窗口,請使用函數'update()'然後'deiconify()'(請參閱博客[「Tkinter:如何顯示/隱藏窗口」](https://www.blog.pythonlibrary。組織/ 2012/7月26日/ Tkinter的,怎麼到顯示隱藏-一個窗口/))。 –

+0

因此,如果我想回到窗口,我會怎麼做?我需要取消它嗎? – ylimes

相關問題