【问题标题】:Using Tkinter iwant to open the second window then close the previous one i was on使用 Tkinter iwant 打开第二个窗口,然后关闭我打开的前一个窗口
【发布时间】:2017-07-16 23:29:30
【问题描述】:

我将如何打开第二个窗口并关闭前一个窗口。因此,使用我的代码,我在第一个窗口上按确定,它会将我带到第二个窗口,但我希望第一个窗口关闭。在过去的2个小时里,我一直在挣扎。谢谢

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

【问题讨论】:

    标签: python windows python-3.x user-interface tkinter


    【解决方案1】:

    提到以下帖子"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 Wages() 时显示主窗口class Welcome(),请添加对函数update() 然后deiconify() 的调用,如下所示:

    Step1 - 将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
    

    【讨论】:

    • 非常感谢,也感谢您提供指向其他帖子的链接!
    • 要再次显示主窗口,请使用函数update(),然后使用deiconify()(参见博客"Tkinter: How to Show / Hide a Window")。
    • 所以如果我想回到窗口我会怎么做呢?我需要取消它吗?
    • 刚刚添加到我的答案中。
    • 非常感谢。那个 python 博客也真的很有帮助
    猜你喜欢
    • 2023-03-31
    • 2021-07-09
    • 1970-01-01
    • 2023-01-12
    • 1970-01-01
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多