处理“线程中的异常..”tkinter,在“messagebox.showerror”中转换

问题描述

当我在 thread (t = Thread (target = self._configure_clicked) 的目标中遇到错误时,它会出现在终端 "Execption in thread Thread-1" 上(我的代码中有更多线程)。

如果出现错误,并且没有错误“messageBox.showinfo”,我希望出现“messageBox.showerror”。我该怎么做?

我附上了 GUI 代码和终端屏幕的摘录

from tkinter import * # importo tutto da tkinter
from tkinter import messageBox,ttk
from threading import Thread
import multi_sensor_acc_gyro1 as multi ##other script.py

...
...

def configure_clicked(self):
        
    t=Thread(target=self._configure_clicked,daemon=True)
    t.start()
    ...
    self.pb.start() #progress bar start
    while t.is_alive():
            self.pb.update() #progress bar update
    ...
    messageBox.showinfo("CONfigURE clicked","the sensors have been configured\n\\n click OK to continue")
    messageBox.showerror("Error","sensor disconnection")
    ...
    ...

def _configure_clicked(self):
    mac_address=["F7:64:55:AD:0A:19","F2:A1:3A:E2:F2:ED","F9:8E:32:DD:1A:EB"]
        
    output_1=multi.configure(mac_address) 
    self.states=output_1[0]
    self.loggers_acc=output_1[1]
    self.loggers_gyro=output_1[2]

...

Error message

我尝试使用 try / except,但我做错了。我没有检测到任何错误

from tkinter import * # importo tutto da tkinter
from tkinter import messageBox,ttk
from threading import Thread
from PIL import Image
import multi_sensor_acc_gyro1 as multi

...
...

def configure_clicked(self):
    try:    
        t=Thread(target=self._configure_clicked,daemon=True)
        t.start()
        ...
        self.pb.start()
        while t.is_alive():
                self.pb.update()
        ...
        messageBox.showinfo("CONfigURE clicked","the sensors have been configured\n\\n click OK to continue")
        ...
        ...
    except:
        messageBox.showerror("Error","sensor disconnection")

def _configure_clicked(self):
    mac_address=["F7:64:55:AD:0A:19","F9:8E:32:DD:1A:EB"]
        
    output_1=multi.configure(mac_address) 
    self.states=output_1[0]
    self.loggers_acc=output_1[1]
    self.loggers_gyro=output_1[2]

...
...

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)