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

问题描述 投票:0回答:1

我在本节中有两个问题。

  1. 在我的代码中,我在root下创建了两个帧,第一帧有“NEXT”按钮进入第二帧。在第二帧中有Run按钮,它已使用close_window函数进行映射。它应该正确关闭所有窗口。但在我的情况下没有关闭它。
  2. 当我点击“运行”时,我需要关闭所有窗口,并需要在同一目录上执行另一个脚本。这可能吗?

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

代码有什么问题?

python tkinter
1个回答
1
投票

“销毁窗口没有正确关闭所有窗口”

也不应该。 destroy方法销毁一个小部件,当一个小部件被销毁时,它的子节点也是如此。

由于frame1frame2都不是“窗户”或窗户的父母,因此没有窗户破坏。


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

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

...
def close_window():
    frame1.quit()
...
root.mainloop()
import another_script
© www.soinside.com 2019 - 2024. All rights reserved.