首页 > 解决方案 > python 脚本不会随着 sys.exit() 停止

问题描述

在我的程序中,我有一个带有进度条的 GUI。进度条的值是另一个脚本的变量,所以我创建了一个刷新 GUI 的更新函数,所以我有一个移动的进度条。

def update(lblhowfar,bar,window,Zeitraum,auswahl,testprogress):# update function to refresh the progress bar and the percentage label
    while rd.filtering_done==False:
        Progress=int(rd.Zähler/rd.Nenner)
        percent=Progress,"%","of","100","%"
        if testprogress < Progress:
            bar['value'] = Progress
            testprogress=Progress
            lblhowfar.config(text=percent)
        window.update()
        time.sleep(.1)
    if rd.filtering_done:# when RelevanteDaten.py is done
        while rd.appending:
            time.sleep(.5)
            continue
        bar['value'] = 100
        percent="100","%","of","100","%"
        lblhowfar.config(text=percent)
        lbl2=Label(window, text="Filtering done",font=("Helvetica Neue 55 Roman",15))
        lbl2.pack()
        window.update()
        RelevanteDaten=rd.RelevanteDaten# Import important data from RelevanteDaten.py
        Continue(RelevanteDaten,lbl2,lblhowfar,bar,window,Zeitraum,auswahl,)
    else:
        window.after(1,update(lblhowfar,bar,window,Zeitraum,auswahl,testprogress))

在 GUI 上,我想要一个可以关闭程序的按钮,但是当我执行该按钮时,我收到一条错误消息。

def Destroy(window,):#shuts down the program
    plt.close(fig="all")
    window.destroy()
    sys.exit()
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\User\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1702, in __call__
    return self.func(*args)
  File "CheckTool.py", line 258, in <lambda>
    button1 = Button(window, text="Use", command=lambda:button_generate(window,checkVariableForCheckBoxList,Zeitraum,),font=("Helvetica Neue 55 Roman",20))
  File "CheckTool.py", line 290, in button_generate
    get_selected_boxes(checkVariableForCheckBoxList,window,Zeitraum,)
  File "CheckTool.py", line 299, in get_selected_boxes
    create_progress_bar(Zeitraum,auswahl,)
  File "CheckTool.py", line 446, in create_progress_bar
    progressscreen.after(1,update(lblhowfar,bar,window,Zeitraum,auswahl,testprogress))
  File "CheckTool.py", line 231, in update
    window.update()
  File "C:\User\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1174, in update
    self.tk.call('update')
_tkinter.TclError: can't invoke "update" command: application has been destroyed

所以显然更新功能仍然运行,虽然它应该与 sys.exit() 停止。正确的?

已经感谢您的帮助。

亲切的问候

标签: pythonpython-3.xtkinter

解决方案


推荐阅读