关闭窗口时出现Python PyDeadObjectError

关闭窗口时出现Python PyDeadObjectError,python,wxpython,Python,Wxpython,这是我的代码(不全是f,因为太长了) [……] 下面是错误消息 Exception in thread Thread-1: Traceback (most recent call last): File "C:\Python27\lib\threading.py", line 551, in __bootstrap_inner self.run() File "C:\Python27\lib\threading.py", line 504, in run self

这是我的代码(不全是f,因为太长了)

[……]

下面是错误消息

    Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python27\lib\threading.py", line 551, in __bootstrap_inner
    self.run()
  File "C:\Python27\lib\threading.py", line 504, in run
    self.__target(*self.__args, **self.__kwargs)
  File "C:\Users\angelo\Documents\books n studies\Projects - Jump starts\chat\Si
ngle node chat resources\single node chat v2.2.1\single node chat v2.2.1u.py", l
ine 197, in data_flow_thread
    self.disconnect()
  File "C:\Python27\lib\site-packages\wx-3.0-msw\wx\_core.py", line 16712, in __
getattr__
    raise PyDeadObjectError(self.attrStr % self._name)
PyDeadObjectError: The C++ part of the TestApp object has been deleted, attribut
e access no longer allowed.

每次我尝试关闭它时都会发生这种情况。完全相同的代码在TKinter上运行得非常好,但由于某些原因,我无法用WX克服它。有什么想法吗?

看起来您是因为线程中出现错误而退出应用程序的。您应该向wxpythongui发送一条消息,告诉它有问题。您可以向用户显示错误消息,也可以在此时退出脚本。除非使用线程安全方法,例如wx.CallAfter或wx.PostEvent,否则无法从GUI调用wxPython中的任何内容

我将使用其中一个来告诉您的GUI它需要退出。我编写了一个小教程,展示了如何在线程中使用wxPython,您可以在这里查看:


很抱歉,我仍然不清楚为什么以及在哪里使用wx.CallAfter。我的意思是,在出现错误时我没有使用GUI,它只是背景代码。其次,如果我将
self.disconnect()
更改为
wx.CallAfter(self.disconnect)
我仍然会得到相同的错误。最后,
try,除了
不会捕获它。更新:如果我更改
if msg==“SPECIAL\u msg\u disconnected\u 70”:self.disconnect()
与:
if msg==“SPECIAL\u msg\u disconnected\u 70”和self.is\u connected\u flag1==True:self.disconnect()
我得到相同的错误,但对于if行。。。
  def data_flow_thread(self):
        """
        NoType -> NoType
        This is the thread wich will start receiving the data after the connection is complete.
        """
        print "inside start_data_flow_thread",self.is_server_flag2 ## DEBUG
        while self.is_running_flag0 == True:
            time.sleep(0.1)
            print 0
            if self.is_server_flag2 == True:
                try:
                    msg = self.conn.recv(512)
                except socket.error, e:
                    sys.exit()
            else:
                try:
                    msg = self.client_s.recv(512)
                except socket.error, e:
                    sys.exit()
            if msg == "SPECIAL_MSG_DISCONECTED_70":
                self.disconnect()
            else:
                self.print_to_screen(msg)

        sys.exit()

    def disconnect(self):
        """
        NoType -> NoType
        This will just stop the connection if there is one.
        """
        print "inside disconnect" ## DEBUG

        try:
            print 1
            self.t3.exit()
        except:
            pass
        time.sleep(0.05)
        try:
            print 2
            self.client_s.send("SPECIAL_MSG_DISCONECTED_70")
            self.client_s.close()
        except:
            try:
                print 3
                self.conn.send("SPECIAL_MSG_DISCONECTED_70")
                self.conn.close()
                self.server_s.close()
            except:
                pass
        self.is_connected_flag1 = False

    def close_main_window(self, event):
        """

        """
        print "Inside close_window" ## DEBUG
        self.is_running_flag0 = False
        time.sleep(0.05)
        self.disconnect()
        print 4
        self.Destroy()
    Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python27\lib\threading.py", line 551, in __bootstrap_inner
    self.run()
  File "C:\Python27\lib\threading.py", line 504, in run
    self.__target(*self.__args, **self.__kwargs)
  File "C:\Users\angelo\Documents\books n studies\Projects - Jump starts\chat\Si
ngle node chat resources\single node chat v2.2.1\single node chat v2.2.1u.py", l
ine 197, in data_flow_thread
    self.disconnect()
  File "C:\Python27\lib\site-packages\wx-3.0-msw\wx\_core.py", line 16712, in __
getattr__
    raise PyDeadObjectError(self.attrStr % self._name)
PyDeadObjectError: The C++ part of the TestApp object has been deleted, attribut
e access no longer allowed.