wxpython中利用线程防止假死的实现方法
作者:mdxy-dxy 时间:2022-01-27 00:45:29
前段时间我编写了一个工业控制的软件,在使用中一直存在一个问题,就是当软件检索设备时,因为这个功能执行的时间比较长,导致GUI界面假死,让用户分辨不清楚软件到底仍在执行,还是真的挂掉了。(虽然我设计了同步log显示,但是这个也同样假死了)
程序截图如下:
代码解析如下:
# -*- coding: utf-8 -*-
import time
import wx
from threading import Thread
from wx.lib.pubsub import Publisher
time库用来执行定时函数,模拟需要长时间执行的功能。Publisher用来在线程间传输消息。
class TestThread(Thread):
def __init__(self):
#线程实例化时立即启动
Thread.__init__(self)
self.start()
def run(self):
#线程执行的代码
for i in range(101):
time.sleep(0.03)
wx.CallAfter(Publisher().sendMessage, "update", i)
time.sleep(0.5)
wx.CallAfter(Publisher().sendMessage, "update", u"线程结束")
TestThread的__init__函数,定义线程实例化时就启动,run函数就是需要长时间执行的程序。
wx.CallAfter与Publisher().sendMessage用来向GUI发送消息。
class MyForm ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Leniy,20140627", pos = wx.DefaultPosition, size = wx.Size( -1,-1 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
gSizer2 = wx.GridSizer( 0, 3, 0, 0 )
self.m_button2 = wx.Button( self, wx.ID_ANY, u"执行线程", wx.DefaultPosition, wx.DefaultSize, 0 )
gSizer2.Add( self.m_button2, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5 )
self.m_staticText2 = wx.StaticText( self, wx.ID_ANY, u"MyLabel", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText2.Wrap( -1 )
gSizer2.Add( self.m_staticText2, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5 )
self.m_gauge1 = wx.Gauge( self, wx.ID_ANY, 100, wx.DefaultPosition, wx.DefaultSize, wx.GA_HORIZONTAL )
self.m_gauge1.SetValue( 0 )
gSizer2.Add( self.m_gauge1, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5 )
self.SetSizer( gSizer2 )
self.Layout()
gSizer2.Fit( self )
self.Centre( wx.BOTH )
self.m_button2.Bind( wx.EVT_BUTTON, self.onButton )
Publisher().subscribe(self.updateDisplay, "update")
def updateDisplay(self, msg):
t = msg.data
if isinstance(t, int):#如果是数字,说明线程正在执行,显示数字
self.m_staticText2.SetLabel("%s%%" % t)
self.m_gauge1.SetValue( t )
else:#否则线程未执行,将按钮重新开启
self.m_staticText2.SetLabel("%s" % t)
self.m_button2.Enable()
def onButton( self, event ):
TestThread()
self.m_staticText2.SetLabel(u"线程开始")
event.GetEventObject().Disable()
这个就是GUI的主程序,__init__中使用Publisher().subscribe(self.updateDisplay, “update”)声明了获取线程消息以及显示的方法。
if __name__ == "__main__":
app = wx.PySimpleApp()
MyForm(None).Show()
app.MainLoop()
最后将窗口MyForm(None).Show()显示出来,程序就完成了。
标签:wxpython,假死,线程
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
导航设计的流行趋势
2007-12-25 12:06:00
![](https://img.aspxhome.com/file/UploadPic/200712/25/2007122512955218s.jpg)
Python查找算法之插补查找算法的实现
2021-03-12 08:16:57
![](https://img.aspxhome.com/file/2023/7/82607_0s.png)
SWF FLASH的param属性参数详解
2008-10-25 15:12:00
Python绘制全球疫情变化地图的实例代码
2022-08-27 07:06:46
![](https://img.aspxhome.com/file/2023/5/66425_0s.gif)
python 利用百度API进行淘宝评论关键词提取
2021-11-14 19:32:36
![](https://img.aspxhome.com/file/2023/5/67835_0s.png)
使用Python给头像戴上圣诞帽的图像操作过程解析
2023-03-17 13:22:01
![](https://img.aspxhome.com/file/2023/4/92304_0s.jpg)
JavaScript 颜色梯度和渐变效果
2009-03-18 11:16:00
![](https://img.aspxhome.com/file/UploadPic/20093/18/ColorGrads-46s.gif)
python base64库给用户名或密码加密的流程
2021-01-30 16:30:39
制作设置404页面
2009-07-03 13:01:00
![](https://img.aspxhome.com/file/UploadPic/20097/3/200973131448261s.jpg)
nlp自然语言处理学习CBOW模型类实现示例解析
2023-08-08 00:26:03
![](https://img.aspxhome.com/file/2023/8/76178_0s.jpg)
Go语言string,int,int64 ,float之间类型转换方法
2023-06-28 15:20:30
css模拟title和alt的提示效果
2007-11-08 13:03:00
![](https://img.aspxhome.com/file/UploadPic/200711/8/2007118131348466s.gif)
使用Python轻松完成垃圾分类(基于图像识别)
2023-06-24 06:06:47
![](https://img.aspxhome.com/file/2023/3/63913_0s.png)
tensorflow实现对图片的读取的示例代码
2023-07-20 06:07:30
![](https://img.aspxhome.com/file/2023/7/63517_0s.jpg)
详解Python中的 type()函数
2023-12-03 09:13:39
Python实现微博动态图片爬取详解
2023-09-14 05:47:25
![](https://img.aspxhome.com/file/2023/0/61730_0s.jpg)
Python实战使用Selenium爬取网页数据
2021-06-18 19:11:56
PL/SQL编程经验小结开发者网络Oracle
2010-07-18 13:27:00
Win7或Windows server 2008中IIS7支持ASP+Access解决方法
2012-03-27 18:31:27
ASP程序中调用函数Now()显示上午下午的问题
2009-08-27 13:09:00