当守护线程中的任何模块 运行 发生异常时如何销毁 tkinter mainloop
How to destroy tkinter mainloop when exception occurs in any module running in daemon thread
我写了一个 Python (3.7) 应用程序,它使用 tkinter 作为 GUI 并在守护线程中运行主要功能。当守护线程发生异常时,有没有办法销毁 tkinter 主循环?
问题是程序有多个模块。如果其中任何一个发生这种情况,有没有办法杀死主循环?否则用户将看到冻结的 GUI。
这是启动线程的代码片段:
import logging
import threading
from tkinter import *
from tkinter import ttk
def main_thread():
if check_input(): # user provided necessary input
program_thread = threading.Thread(target=program_pipeline) # runs main
program_thread.daemon = True # daemon thread can be killed anytime?
program_thread.start()
block_user_entries()
clear_results() # from a previous run
else:
logging.info("\n--------------- TRY AGAIN -------------------\n")
ublock_user_entries()
program_pipeline
与多个模块和包通信
线程在用户单击按钮时启动
analyze_button = ttk.Button(frame, text="Analyze", state="normal", command=main_thread)
analyze_button.grid(column=2, row=0, pady=2, sticky=(W))
root.mainloop()
如果我对此的解释正确,那么您正在寻找一种从守护进程中断主线程的方法。
现在,通常这不是推荐的选项,您可以使用低级别来做同样的事情 _thread.interrupt_main()
。
如果您提供更多信息,可以想到更好的解决方案。
import _thread
import threading
import tkinter as tk
def program_pipeline():
try:
# DO STUFF
# calling other modules that may raise exception/error
raise ValueError("Error")
except BaseException as be:
print("Exception in daemon", be)
_thread.interrupt_main()
def main_thread():
program_thread = threading.Thread(target=program_pipeline)
program_thread.daemon = True
program_thread.start()
root = tk.Tk()
analyze_button = tk.Button(root, text="Analyze", state="normal", command=main_thread)
analyze_button.grid(column=2, row=0, pady=2, sticky=tk.W)
try:
root.mainloop()
except KeyboardInterrupt as kie:
print("exception in main", kie)
我写了一个 Python (3.7) 应用程序,它使用 tkinter 作为 GUI 并在守护线程中运行主要功能。当守护线程发生异常时,有没有办法销毁 tkinter 主循环?
问题是程序有多个模块。如果其中任何一个发生这种情况,有没有办法杀死主循环?否则用户将看到冻结的 GUI。
这是启动线程的代码片段:
import logging
import threading
from tkinter import *
from tkinter import ttk
def main_thread():
if check_input(): # user provided necessary input
program_thread = threading.Thread(target=program_pipeline) # runs main
program_thread.daemon = True # daemon thread can be killed anytime?
program_thread.start()
block_user_entries()
clear_results() # from a previous run
else:
logging.info("\n--------------- TRY AGAIN -------------------\n")
ublock_user_entries()
program_pipeline
与多个模块和包通信
线程在用户单击按钮时启动
analyze_button = ttk.Button(frame, text="Analyze", state="normal", command=main_thread)
analyze_button.grid(column=2, row=0, pady=2, sticky=(W))
root.mainloop()
如果我对此的解释正确,那么您正在寻找一种从守护进程中断主线程的方法。
现在,通常这不是推荐的选项,您可以使用低级别来做同样的事情 _thread.interrupt_main()
。
如果您提供更多信息,可以想到更好的解决方案。
import _thread
import threading
import tkinter as tk
def program_pipeline():
try:
# DO STUFF
# calling other modules that may raise exception/error
raise ValueError("Error")
except BaseException as be:
print("Exception in daemon", be)
_thread.interrupt_main()
def main_thread():
program_thread = threading.Thread(target=program_pipeline)
program_thread.daemon = True
program_thread.start()
root = tk.Tk()
analyze_button = tk.Button(root, text="Analyze", state="normal", command=main_thread)
analyze_button.grid(column=2, row=0, pady=2, sticky=tk.W)
try:
root.mainloop()
except KeyboardInterrupt as kie:
print("exception in main", kie)