wxPythonとthreading


テキストエディタでは、シンタックスハイライトの処理が重い。

OnPaintメソッド内で構文解析をしていたのでは、間に合わない。

だから、別のスレッドで平行的に解析してみよう。



とりあえず、wxPythonとthreadingを併用して問題がないか試してみた。

#encoding:shift-jis
import time
import wx
import threading

class Counter(threading.Thread):
  def __init__(self, frame):
      threading.Thread.__init__(self)
      self.is_stop = False
      self.frame = frame

  def stop(self):
      self.is_stop = True
      self.join()
    
  def run(self):
      count = 0
      while not self.is_stop:
          self.frame.SetTitle(unicode(count))
          time.sleep(0.1)
          count += 1

class Frame(wx.Frame):
  def __init__(self):
      wx.Frame.__init__(self, None, -1)
      self.button = wx.Button(self, -1, u"Reset")
      self.button.Bind(wx.EVT_BUTTON, self.reset)
      self.counter = Counter(self)
      self.counter.start()
    
      self.Bind(wx.EVT_CLOSE, self.OnClose)
  def reset(self, event):
      self.counter.stop()
      self.counter = Counter(self)
      self.counter.start()

  def OnClose(self, event):
      self.counter.stop()
      self.Destroy()
    
def main():
  app = wx.PySimpleApp()
  f = Frame()
  f.Show()
  app.MainLoop()

if __name__ == '__main__':
  main()