如何在 wxPython 中处理多个 EVT_TEXT 事件? [英] How do I handle multiple EVT_TEXT events in wxPython?
问题描述
这是两部分问题的一部分(另一部分是 这里)
所以这就是我要寻找的:绑定到文本控件的 EVT_TEXT
事件的函数,该函数等待几秒钟,然后在延迟时间结束时调用另一个函数.这很容易,但是,每次生成新的 EVT_TEXT
事件时,我都希望它重置延迟时间.我正在寻找的效果是让用户输入文本控件,然后在我假设他们完成后,我运行这个问题的另一部分中描述的函数,该函数对他们所写的内容进行拼写检查.>
所以我尝试的简单方法是这样的:
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 <1.0:打印 self.count时间.睡眠(0.1)self.count += 0.1wx.PostEvent(self.panel, ResultEvent())def newSig(self):打印新"self.count = 0
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 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
这篇关于如何在 wxPython 中处理多个 EVT_TEXT 事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!