版权所有,转载请注明出处:http://guangboo.org/2013/08/23/wxpython-non-gui-thread-call-gui-method

之前有介绍了《wxPython多线程支持》,包含了wx.CallAfter, wx.PostEvent, wxCallLater的用法,本文将wx.CallAfter封装成decorator,使得UI线程中的方法可以在非UI线程中调用。

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

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()

源代码下载:demo.py

wxPython跨线程调用的更多相关文章

  1. C# 跨线程调用控件

    在C# 的应用程序开发中, 我们经常要把UI线程和工作线程分开,防止界面停止响应.  同时我们又需要在工作线程中更新UI界面上的控件, 下面介绍几种常用的方法 阅读目录 线程间操作无效 第一种办法:禁 ...

  2. C# 跨线程调用问题

    纠结了好久,终于知道了winform和WPF的UI的跨线程调用的解决方法: winform下如果为了省事,可以直接禁用跨线程检查: Control.CheckForIllegalCrossThread ...

  3. c#中如何跨线程调用windows窗体控件

    c#中如何跨线程调用windows窗体控件?   我们在做winform应用的时候,大部分情况下都会碰到使用多线程控制界面上控件信息的问题.然而我们并不能用传统方法来做这个问题,下面我将详细的介绍.首 ...

  4. c#中如何跨线程调用windows窗体控件?

    我们在做winform应用的时候,大部分情况下都会碰到使用多线程控制界面上控件信息的问题.然而我们并不能用传统方法来做这个问题,下面我将详细的介绍.首先来看传统方法: public partial c ...

  5. 如何跨线程调用Windows窗体控件

    通过一个子线程来操作主线程中的控件,但是,这样作会出现一个问题(如图1所示),就是TextBox控件是在主线程中创建的,在子线程中并没有对其进行创建,也就是从不是创建控件的线程访问它.那么,如何解决跨 ...

  6. 关于Qt跨线程调用IO子类的理解

    一.疑问 突然想到,类似于QTcpsocket和QSerialport这类对象,如果是在A线程中new的,那就不能在其他线程中访问.我一般是这样做的: 封装一个QObject子类,放这些对象进去,然后 ...

  7. MFC、Qt、C#跨线程调用对象

    MFC.Qt.C#都是面向对象的编程库 1.MFC不允许跨线程调用对象,即线程只能调用它本身分配了空间的对象 In a multi-threaded application written using ...

  8. Qt跨线程调用错误解析及解决办法

    错误提示:Error: Cannot create children for a parent that is in a different thread. 错误案例分析 新建SerialLink子线 ...

  9. Visual studio之C#跨线程调用UI控件

    背景 当前串口通讯项目,多个线程需要同时利用richTextBoxMsg控件打印信息,直接调用会造成线程不安全,严重的时候会直接导致UI线程挂掉,因此本篇就跨线程调用UI控件做个记录. 正文 定义控件 ...

随机推荐

  1. 自己编写SqlHelper

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  2. PHPexcel数据按模板导出

    <?php header("Content-type: text/html; charset=gb2312"); error_reporting(E_ALL); ini_se ...

  3. leetcode修炼之路——387. First Unique Character in a String

    最近公司搬家了,有两天没写了,今天闲下来了,继续开始算法之路. leetcode的题目如下: Given a string, find the first non-repeating characte ...

  4. android - startActivity浅谈

    当执行startActivity(Intent intent, Bundle options)函数的时候,应用程序不是直接呼叫另外一个Activity,而是将intent传进Android框架中.An ...

  5. MPICH2在两台Ubuntu上安装

    本文在经过大量的实验终于不负众望成功的在两台Ubuntu 12.04上部署MPI的一个小型集群,MPICH2所用版本为mpich2-1.4.1,下载地址:http://www.mcs.anl.gov/ ...

  6. Spring AOP原理及拦截器

    原理 AOP(Aspect Oriented Programming),也就是面向方面编程的技术.AOP基于IoC基础,是对OOP的有益补充. AOP将应用系统分为两部分,核心业务逻辑(Core bu ...

  7. Java学习-----单例模式

    一.问题引入 偶然想想到的如果把Java的构造方法弄成private,那里面的成员属性是不是只有通过static来访问呢:如果构造方法是private的话,那么有什么好处呢:如果构造方法是privat ...

  8. 关于jQuery表单校验的应用

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  9. java计算一个月有多少天和多少周

    import java.util.Calendar; /** * 功能概述:计算指定年月的天数和周数<br> * 创建时间:2010-5-17 下午05:25:58<br> * ...

  10. 封装好的PHP分页类,简单好用--在开源看到的,取回来自己用

    class Pagination  独立分页类 调用方式: $pagenation = new Pagination( 4, 10, 200 ); // 4(第一个参数) = currentPage, ...