【问题标题】:Destroy window not closing all windows properly销毁窗口未正确关闭所有窗口
【发布时间】:2023-03-24 11:35:01
【问题描述】:

我在这部分有两个查询。

  1. 在我的代码中,我在根目录下创建了两个框架,第一个框架有“NEXT”按钮可以进入第二个框架。在第二帧有运行按钮,它映射了 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】:

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

也不应该。 destroy 方法会销毁一个小部件,当一个小部件被销毁时,它的子级也会被销毁。

由于 frame1frame2 都不是 'windows' 或窗口的父级,因此不会发生窗口破坏。


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

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

...
def close_window():
    frame1.quit()
...
root.mainloop()
import another_script

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-31
    • 1970-01-01
    • 2020-02-22
    • 2011-01-18
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    • 1970-01-01
    相关资源
    最近更新 更多