简体   繁体   English

当消息框(例如错误消息)出现/打开时,如何强制 window 关闭?

[英]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.

相关问题 出现消息框时,如何禁用我的 TopLevel window? - How can I disable my TopLevel window when a message box appears? Python、flask、babel-消息和例如西班牙语 - 如何 - Python, flask, babel- message, and e.g. Spanish - How to 扭曲 &gt; 如何从 window TCP 帧长度(例如 1380 字节)读取 TCP 消息 - Twisted > How to read a TCP message longer than TCP frame-length e.g. 1380 bytes from a window TCP client 打印到 Tkinter Window 而不是程序 output 框(例如 PyCharm) - Printing to Tkinter Window and not to the program output box (e.g. PyCharm) 如何将字符串(例如'A')引用到更大列表的索引(例如['A','B','C','D',...])? - How can I reference a string (e.g. 'A') to the index of a larger list (e.g. ['A', 'B', 'C', 'D', ...])? 在 PySimpleGUI 中创建 window 布局时出现错误“您的行不是可迭代的(例如列表)” - Error "Your row is not an iterable (e.g. a list)" when creating window layout in PySimpleGUI Matplotlib 在绘图时似乎不使用 rcparams,特别是对于文本(例如,默认情况下标题为粗体) - Matplotlib appears to not use rcparams when plotting, particularly for text (e.g. titles are bold when default is not) 如何判断我是否已在 VS 代码中成功导入了 Python 包(例如 Pandas)? - How can I tell if I have successfully imported a package for python (e.g. Pandas) in VS code? 我可以使用例如 scipy griddata 创建和插值吗? - Can I create and interpolant using e.g. scipy griddata? 如何使TensorFlow 2.0处理分段渐变(例如跨越`tf.gather`)? - How can I make TensorFlow 2.0 handle piecewise gradients (e.g. across `tf.gather`)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM