受威胁的方法未显示Traitsui / pyface窗口

时间:2020-10-12 10:19:23

标签: python python-multithreading traitsui pyface

如果我启动的线程中发生异常,我需要显示一条错误消息。我的问题是,即使消息似乎是以某种方式创建的,也不会显示该消息。主窗口被冻结,直到我按Enter键“消除”错误消息为止。

以下代码段是我的实际代码的简化版本,用于说明问题。

import threading
from traits.api import *
from traitsui.api import *

from pyface.api import error


class Main(HasTraits):

    go=Button("GO")
    
    traits_view = View(Item("go"))
    
    error = Str("") 
    
    def _go_fired(self):
    
        thread = threading.Thread(target=self.go_exec)
        thread.start()


    def go_exec(self):
    
        try:
        
            # here i need to execute some code than can produce an 
            raise Exception("error")

        except Exception as ex:
            self.error=ex.args[0]
                
    def _error_changed(self):
        error(None, self.error)
        
m=Main()
m.configure_traits()

最后我收到此消息

QBasicTimer::start: Timers cannot be started from another thread
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7ff5ffc03630), parent's thread is QThread(0x7ff5ffe8a7f0), current thread is QThread(0x7ff602034060)

如何“强制”显示消息?

在此先感谢您的帮助!

0 个答案:

没有答案