销毁窗口未正确关闭所有窗口

时间:2018-03-06 17:33:54

标签: python tkinter

我在本节中有两个问题。

  1. 在我的代码中,我在root下创建了两个帧,第一帧有“NEXT”按钮进入第二帧。在第二帧中有Run按钮,它已使用close_window函数进行映射。它应该正确关闭所有窗口。但在我的情况下没有关闭它。

  2. 当我点击“运行”时,我需要关闭所有窗口并需要在同一目录上执行另一个脚本。这可能吗?

  3. from Tkinter import *
    
    
    def close_window():
        frame2.destroy()
        frame1.destroy()
    
    
    def swap_frame(frame):
        frame.tkraise()
    
    
    root = Tk()
    root.geometry("900x650+220+20")
    root.title("Testing")
    root.configure(borderwidth="1", relief="sunken", cursor="arrow", background="#dbd8d7", highlightcolor="black")
    root.resizable(width=False, height=False)
    
    frame2 = Frame(root, width=900, height=650)
    frame1 = Frame(root, width=900, height=650)
    
    Button1 = Button(frame1, text="Next", width=10, height=2, bg="#dbd8d7", command=lambda: swap_frame(frame2))
    Button1.place(x=580, y=580)
    
    Button2 = Button(frame2, text="Run", width=10, height=2, bg="#dbd8d7", command=close_window,)
    Button2.place(x=580, y=580)
    
    
    frame2.grid(row=0, column=0)
    frame1.grid(row=0, column=0)
    
    root.mainloop()
    

    代码有什么问题?

1 个答案:

答案 0 :(得分:1)

  

"销毁窗口未正确关闭所有窗口"

也不应该。 destroy方法会销毁一个小部件,当一个小部件被销毁时,它的子节点就会被破坏。

由于frame1frame2都不是' windows' 或窗口的父母,因此不会破坏窗口的地方。

  

"当我点击"Run"时,我需要关闭所有窗口并需要在同一目录中执行另一个脚本。这可能吗?"

有可能。在任何GUI对象上使用quit,而不是destroy。它会停止mainloop,从而破坏整个GUI。因此它也解决了第一个问题。然后导入another_script

...
def close_window():
    frame1.quit()
...
root.mainloop()
import another_script
相关问题