如何在消息框(例如错误消息)appears/opens 时强制关闭 window?

How can I force a window to close when a message box (e.g. an error message) appears/opens?

所以我的代码中有一个按钮可以做两件事:它启动一个需要一段时间的进程,它还打开一个 window 带有一个进度条,该进度条一直有效,直到另一个进程完成,然后它关闭。我想让它(带有进度条的window)在出现消息框时也将关闭。这样,如果漫长的过程搞砸了,进度条就不会永远 运行。下面是我的代码片段(其他所有未显示的内容均按我希望的方式工作):

def progress_bar():
    info_window = Toplevel(main_window)
    info_window.title("Progress_Bar")
    info_window.geometry("200x50")
    progress = Progressbar(info_window, orient=HORIZONTAL, length=200, mode='indeterminate')
    label3 = Label(info_window, text="Loading...")
    label3.pack() 
    while true != "True":
        progress.pack()
        progress['value'] += 1
        info_window.update_idletasks()
        time.sleep(0.01)
    if progress['value'] == 100:
        progress['value'] = 0
    if true == "True":
        info_window.destroy()
    if show_error_message() == True:   # <-- This is where I'm unsure; this statement
        info_window.destroy()          # Obviously doesn't work but I don't know
                                       # exactly how to phrase it correctly to make it work
def show_error_message():
    messagebox.showerror("ERROR 4343", "KILLIN' IT")  # <-- this occurs when I press a different button 
                                                      

所以基本的想法是:进度条 window 在按下一个按钮时打开,运行s 直到在按下另一个按钮时出现错误消息。

也许您可以做的是,在显示错误后,调用一个函数来关闭 window:

def closewindow():
    #do something like saving before closure
    info_window.destroy()
    self.root.destroy() #if you also want to close the main window

def show_error_message():
    messagebox.showerror("ERROR 4343", "KILLIN' IT") 
    closewindow()