2013-12-09 31 views
1

這是一個問題的兩個部分中的一部分(另一部分是here如何處理wxPython中的多個EVT_TEXT事件?

所以這裏就是我在尋找:這勢必給EVT_TEXT事件等待幾秒鐘文本控件的功能,則在延遲時間結束時調用另一個函數。這很容易,但是,我希望它在每次生成新的EVT_TEXT事件時重置延遲時間。我正在尋找的效果是在文本控件中輸入用戶類型,然後在我認爲它們完成後,我運行此問題的其他部分中描述的函數,該函數將檢查他們所寫的內容。

所以簡單的辦法我想是這樣的:

def OnEdit(self, event): 
    for i in range(0,3): 
     print i 
     time.sleep(1) 

然而,這只是迫使3秒的等待,不管是什麼。如何「打入」此功能來重置計數器?提前致謝。

編輯:原來是這樣做的方式與線程。 YIPPEE

回答

1

的滿穿的答案,建有this教程的幫助:

from threading import * 
import wx 
import time 

EVT_RESULT_ID = wx.NewId() 

def EVT_RESULT(win, func): 
    win.Connect(-1, -1, EVT_RESULT_ID, func) 

class MyGui(wx.Frame): 
    def __init__(self): 
     self.spellchkthrd = None 
     #lots of stuff 

     self.input = wx.TextCtrl(self.panel, -1, "", size=(200, 150), style=wx.TE_MULTILINE|wx.TE_LEFT|wx.TE_RICH)   
     self.Bind(wx.EVT_TEXT, self.OnEdit, self.input) 
     EVT_RESULT(self, self.OnSplCheck)  

    def OnEdit(self, event): 
     if not self.spellchkthrd: 
      self.spellchkthrd = SpellCheckThread(self) 
     else: 
      self.spellchkthrd.newSig() 

    def OnSplCheck(self, event): 
     self.spellchkthrd = None 
     #All the spell checking stuff 

class ResultEvent(wx.PyEvent): 
    def __init__(self): 
     wx.PyEvent.__init__(self) 
     self.SetEventType(EVT_RESULT_ID) 

class SpellCheckThread(Thread): 
    def __init__(self, panel): 
     Thread.__init__(self) 
     self.count = 0 
     self.panel = panel 
     self.start() 

    def run(self): 
     while self.count < 1.0: 
      print self.count 
      time.sleep(0.1)    
      self.count += 0.1 

     wx.PostEvent(self.panel, ResultEvent()) 

    def newSig(self): 
     print "new" 
     self.count = 0 
相關問題