如何在不终止进程的情况下关闭 Tkinter window?

How can I close a Tkinter window without killing my process?

我有一个用 Tkinter 编写的可视化工具。通常,我会独立执行它,然后在完成后手动关闭 window。但是,我想从另一个 Python 程序调用它,执行主循环一次(并将 canvas 保存为 SVG),然后关闭 window,允许程序继续。
如果我什至懒得打开 window,只是重新使用我的代码来绘制 SVG,那也行。

我的 tk 应用程序如下所示:

class MainApplication(tk.Frame):

    def __init__(self, output_path, parent=None):
        tk.Frame.__init__(self, parent)
        self.parent = parent
        # draw visualization, save as svg
        #...
        #



在我的代码的另一部分,我调用

kill = True

root = tk.Tk()
root.title("Placement Visualizer")
MainApplication(output_path, root ).pack(side="top", fill="both", expand=True)
    
if kill:
    root.destroy()
root.mainloop()

我收到此错误:Tcl_AsyncDelete: async handler deleted by the wrong thread Aborted (core dumped) 我试过使用 root.quit() 或删除 root.mainloop(),但我没有得到想要的结果。 谢谢

在“我的代码的另一部分”中:

if kill:
    root.quit()
else:
    root.mainloop()

完全不打开 window,而是绘制所有内容并保存 SVG,只需调用 root.quit()。要正常打开 window(并且必须关闭它,终止进程),请调用 root.mainloop()。