[英]How can I force a window to close when a message box (e.g. an error message) appears/opens?
So I have a button in my code that does two things: it starts a process which takes a while, and it also opens a window with a progress bar that works until the other process finishes, then it closes.所以我的代码中有一个按钮,它做了两件事:它启动一个需要一段时间的进程,它还打开一个 window 和一个进度条,该进度条一直工作到另一个进程完成,然后关闭。 I want to make it so it (the window with the progress bar) will also close if a message box appears.我想让它(带有进度条的 window)也会在出现消息框时关闭。 That way if the long process gets screwed up, the progress bar won't run forever.这样,如果漫长的过程搞砸了,进度条就不会永远运行。 A snippet of my code is below (everything else not shown works the way I want it to):下面是我的代码片段(未显示的所有其他内容都按我想要的方式工作):
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
so the basic idea: progress bar window opens at the push of one button and runs until the error message occurs at the push of another button.所以基本的想法:进度条 window 在按下一个按钮时打开并运行,直到在按下另一个按钮时出现错误消息。
Maybe what you can do is, after the error is shown, call a function that closes the window:也许您可以做的是,在显示错误后,调用关闭 window 的 function:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.