2014-11-23 103 views
2

我在wxPython中創建了一個程序。它有一個EVT.ACTIVATE其中觸發​​函數。一切正常,直到我在​​函數中添加一些條件。如果出現中頻條件,程序將顯示:'pythonw.exe'在退出時已停止工作。讓我來解釋一下代碼:程序在退出時崩潰

# 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 

看在Focus功能.IsChecked條件下,它會導致問題。想我怎麼確定?因爲當我刪除IF條件程序工作得很好。所以我必須寫這樣的功能,防止錯誤:

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

但我需要應用條件,使我的程序的用戶友好的,爲什麼我的程序停止退出時響應爲條件?錯誤的細節可能會有所幫助,所以我附上:

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 
+1

'self.menu_clip'是否定義在'if'的位置? – Werner 2014-11-24 10:43:03

+0

@Werner是的。我已經找到了解決方案,我自己的答案給出。 – 2014-11-25 19:23:57

回答

0

好吧,我自己找到了解決方案。該函數應該這樣寫的,以防止碰撞聲明:

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) 

在這裏我補充e.GetActive,當窗口接收焦點返回true,而不是在它失去焦點。 可能的原因崩潰:我認爲,當我關上窗戶失去了焦點,我以前的功能是不適當的,以處理這種情況。因爲該窗口已經被銷燬,函數應該在其上工作。也許這是導致崩潰的原因。添加e.GetActive可以處理這個錯誤。