簡體   English   中英

當消息框(例如錯誤消息)出現/打開時,如何強制 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 在按下一個按鈕時打開並運行,直到在按下另一個按鈕時出現錯誤消息。

也許您可以做的是,在顯示錯誤后,調用關閉 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM