当前位置:首页 > 商讯 >

wxPython跨线程调用

作者:永年县聚联紧固件制造有限公司    发布时间:2017-09-07 13:28:09

wxPython跨线程调用

本文将wx.CallAfter方法进行了封装,代码如下:

import wx def call_after(func): def _wrapper(*args, **kwargs): return wx.CallAfter(func, *args, **kwargs) return _wrapper import wx def call_after(func): def _wrapper(*args, **kwargs): return wx.CallAfter(func, *args, **kwargs)

return _wrapper当UI线程中的方法需要被非UI线程调用时,只有使用call_after进行修饰即可。如下对《wxPython多线程》中的实例改进:

import time import wx from threading import Thread from wx.lib.pubsub import Publisher def call_after(func): def _wrapper(*args, **kwargs): return wx.CallAfter(func, *args, **kwargs) return _wrapper class MyForm(wx.Frame): #---------------------------------------------------------------------- def __init__(self): wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial") # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self.displayLbl = wx.StaticText(panel, label="Amount of time since thread started goes here") self.btn = btn = wx.Button(panel, label="Start Thread") btn.Bind(wx.EVT_BUTTON, self.onButton) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(self.displayLbl, 0, wx.ALL|wx.CENTER, 5) sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5) panel.SetSizer(sizer) self._thread = Thread(target = self.run, args = ()) self._thread.daemon = True def run(self): i = 0 while True: i += 1 time.sleep(1) self.updateDisplay('Seconds: %d' % i) #---------------------------------------------------------------------- def onButton(self, event): """ Runs the thread """ self._thread.start() self.started = True self.displayLbl.SetLabel("Thread started!") btn = event.GetEventObject() btn.Disable() @call_after def updateDisplay(self, msg): """ Receives data from thread and updates the display """ self.displayLbl.SetLabel(msg) #---------------------------------------------------------------------- # Run the program if __name__ == "__main__": app = wx.PySimpleApp() frame = MyForm().Show() app.MainLoop()

,专题,采集软件

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:襄阳网站制作 http://xiangyang.666rj.com

  • 上一篇:新浪博客案例分析 让博客推广回归真相
  • 下一篇:最后一页
  •