关闭大型机时出现wxPython,运行时和断言错误

时间:2019-09-04 16:29:38

标签: multithreading python-2.7 user-interface wxpython

我有一个运行一些线程的GUI,到目前为止,只有四个。其中两个正在运行读取路由,从仪器到完成上载到SQL。他们中的另外两个正在监视要上传到SQL数据库的数据。关闭大型机时,我收到一个Runtimeerror,它们都更新了GUI。

Exception in thread Thread-3:
Traceback (most recent call last):
  File "C:\Python27\lib\threading.py", line 801, in __bootstrap_inner
    self.run()
  File "C:\Users\ACAS\PycharmProjects\ACAS\Main\GUI\panels\hygrometer_panel.py", line 42, in run
    wx.PostEvent(self.wxObject, ResultEvent(self.worker.read()))
RuntimeError: wrapped C/C++ object of type hygrometer has been deleted

我尝试使用wx.CallAfter行。但是该错误仅更改为断言错误。

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python27\lib\threading.py", line 801, in __bootstrap_inner
    self.run()
  File "C:\Users\ACAS\PycharmProjects\ACAS\Main\GUI\panels\acquisition_panel.py", line 20, in run
    self.method()
  File "C:\Users\ACAS\PycharmProjects\ACAS\Main\GUI\panels\acquisition_panel.py", line 73, in ccn_status_method
    wx.CallAfter(self.ccn_status.SetLabel, 'RUNNING')
  File "C:\Python27\lib\site-packages\wx\core.py", line 3254, in CallAfter
    assert app is not None, 'No wx.App created yet'
AssertionError: No wx.App created yet

这里有一些线程方法。 首先

class TestThread(Thread):
    def __init__(self, wxObject):
        """Init Worker Thread Class."""
        Thread.__init__(self)
        self.wxObject = wxObject
        self.worker = workers.hygrometer_worker()
        self.start()  # start the thread

    def run(self):
        """Run Worker Thread."""
        # This is the code executing in the new thread.
        while True:
            wx.PostEvent(self.wxObject, ResultEvent(self.worker.read()))
            time.sleep(1)

第二

class TestThread(Thread):
    def __init__(self, method):
        """Init Worker Thread Class."""
        Thread.__init__(self)
        self.method = method
        self.start()  # start the thread

    def run(self):
        """Run Worker Thread."""
        # This is the code executing in the new thread.
        while True:
            self.method()
            time.sleep(1)

这发送了完成所有工作的方法,

    def update_display(self):
        data = self.worker.read()
        index = data.index.values[0]
        wx.CallAfter(self.current_ss_value.SetLabel, str(data.at[index, 'Current SS']))
        wx.CallAfter(self.stage_value.SetLabel, str(data.at[index, '1st Stage Mon']))
        wx.CallAfter(self.concentration_value.SetLabel, str(data.at[index, 'CCN Number Conc']))
        wx.CallAfter(self.delta_value.SetLabel, str(data.at[index, 'Delta T']))
        wx.CallAfter(self.cmd_value.SetLabel, str(data.at[index, 'CMD']))
        wx.CallAfter(self.gmd_values.SetLabel, str(data.at[index, 'GMD']))

我似乎并没有真正的错误。我尝试了不同的事情。

1 个答案:

答案 0 :(得分:1)

通过使用self.wxObject进行测试,可以确保在尝试使用if之前仍然存在。窗口小部件类具有__int____bool__,如果C ++对象已被破坏,则返回False。所以,像这样:

    if self.wxObject:
        wx.PostEvent(self.wxObject, ...)

对于wx.App错误,您也可以使用wx.GetApp()进行测试,并确保不是None,然后再使用wx.CallAfter。或者,您也可以将工作线程的启动延迟到创建应用程序对象之后。

关于您的代码的另一条评论:我建议连续发出多个wx.CallAfters,每个发一个单独的UI更新。最好将这些语句移至新方法,然后使用wx.CallAfter来仅调用该方法。这样更有效,但是更重要的是,它避免了事件系统的实现方式以及在较早的事件返回之前执行其中某些CallAfter的问题。