Search code examples
pythonwxpythonexit

program crashes when exiting


I have created a program in wxPython. It has a EVT.ACTIVATE which fires self.Focus function. Everything works fine till I add some conditions in the self.Focus function. If there's an IF condition, the program shows: 'pythonw.exe' has stopped working on exit. Let me explain the code:

# partial code of my program

self.Bind(wx.EVT_ACTIVATE, self.Focus)

def FindButton(self, e):
        Input = self.search.GetValue()
        # starts a thread that queries a big database
        t = ProcessThread(str(Input), self.output1, self.output2, self.Object, self.container)
        t.start()

def Focus(self, e):

    # handles the paste command when window gets activated
    try:
        if self.menu_clip.IsChecked(): # this is the condition that causes problems
            self.search.SetValue('')
            self.search.Paste()
            self.FindButton(None)
    except:
        pass

Look at the .IsChecked condition in the Focus function, it causes the problem. Thinking how am I sure? Becuase when I remove that IF condition program works just fine. So I have to write the function in this way to prevent the error:

def Focus(self, e):
    # handles the paste command when window gets activated
    try:
        self.search.SetValue('')
        self.search.Paste()
        self.FindButton(None)
    except:
        pass

But I need to apply that condition to make my program user-friendly, why my program stops responding for that condition when exiting? The error details may help, so I am attaching that:

Problem signature:
  Problem Event Name:   APPCRASH
  Application Name: pythonw.exe
  Application Version:  0.0.0.0
  Application Timestamp:    4f84a6ca
  Fault Module Name:    StackHash_0a9e
  Fault Module Version: 0.0.0.0
  Fault Module Timestamp:   00000000
  Exception Code:   c0000005
  Exception Offset: 4d8dcccc
  OS Version:   6.1.7600.2.0.0.256.1
  Locale ID:    1033
  Additional Information 1: 0a9e
  Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
  Additional Information 3: 0a9e
  Additional Information 4: 0a9e372d3b4ad19135b953a78882e789

Solution

  • Ok, I myself have found a solution for this. The function should be written in this way to prevent the stated crash:

    def Focus(self, e):
        # handles the paste command when window gets activated
        if e.GetActive() and self.menu_clip.IsChecked():
            self.search.SetValue('')
            self.search.Paste()
            self.FindButton(None)
    

    Here I add e.GetActive, it returns true when the window RECEIVES focus, but not when it loses focus. Possible reason of crash: I think, when I closed the window it lost focus, and my previous function was improper to handle that situation. Because the window is already destroyed on which the function should work on. Maybe that's the cause of crash. Adding e.GetActive can handle this error.