所以我尝试的简单方法是这样的:def OnEdit(self, event):对于范围内的 i (0,3):打印我时间.睡眠(1)但是,无论如何,这只会强制等待 3 秒.如何闯入"此功能以重置计数器?提前致谢. 原来这样做的方法是使用线程.伊皮 解决方案 在 的帮助下构建的完整线程答案这个教程:from threading import *进口 wx导入时间EVT_RESULT_ID = wx.NewId()def EVT_RESULT(win, func):win.Connect(-1, -1, EVT_RESULT_ID, func)类 MyGui(wx.Frame):def __init__(self):self.spellchkthrd = 无#很多东西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.OnSplCheck)def OnEdit(self, event):如果不是 self.spellchkthrd:self.spellchkthrd = SpellCheckThread(self)别的:self.spellchkthrd.newSig()def OnSplCheck(self, event):self.spellchkthrd = 无#所有拼写检查的东西类结果事件(wx.PyEvent):def __init__(self):wx.PyEvent.__init__(self)self.SetEventType(EVT_RESULT_ID)类 SpellCheckThread(线程):def __init__(self, panel):Thread.__init__(self)self.count = 0self.panel = 面板self.start()定义运行(自我):而 self.count This is one part of a two part question (other part is here)So here's what I'm looking for: A function which is bound to the EVT_TEXT event of a text control that waits a few seconds, then calls another function at the end of the delay time. Thats easy enough, but, I'd like it to reset the delay time every time a new EVT_TEXT event is generated. The effect I'm looking for is to have a user type into the text control, then after I assume they're done, I run the function described in the other part of this question which spell checks what they've written.So the simple approach I tried was this:def OnEdit(self, event): for i in range(0,3): print i time.sleep(1)However, this just forces a 3 second wait, no matter what. How do I "break in" to this function to reset the counter? Thanks in advance.EDIT: Turns out the way to do this was with threading. Yippee 解决方案 The full threading answer, built with the help of this tutorial:from threading import *import wximport timeEVT_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 stuffclass 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 这篇关于如何在 wxPython 中处理多个 EVT_TEXT 事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 11:26