How do I handle multiple EVT_TEXT events in wxPython?

岁酱吖の 提交于 2019-12-11 13:59:32

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/20479459/how-do-i-handle-multiple-evt-text-events-in-wxpython

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!