2017-02-26 164 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

相关问题