在wxPython中使用多线程
来源:互联网 发布:linux halt命令 编辑:程序博客网 时间:2024/06/06 00:27
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import wx
import threading
import random
class WorkerThread(threading.Thread):
'''
This just simulates some long-running task that periodically sends a message to the GUI thread.
'''
def __init__(self, threadNum, window):
threading.Thread.__init__(self)
self.threadNum = threadNum
self.window = window
self.timeToQuit = threading.Event()
self.timeToQuit.clear()
self.messageCount = random.randint(10,20)
self.messageDelay = 0.1 + 2.0 * random.random()
def stop(self):
self.timeToQuit.set()
def run(self):#运行一个线程
msg = 'Thread %d iterating %d times with a delay of %1.4f\n'%(self.threadNum, self.messageCount, self.messageDelay)
wx.CallAfter(self.window.LogMessage, msg)
for i in range(1, self.messageCount+1):
self.timeToQuit.wait(self.messageDelay)
if self.timeToQuit.isSet():
break
msg = 'Message %d from thread %d\n' % (i, self.threadNum)
wx.CallAfter(self.window.LogMessage, msg)
else:
wx.CallAfter(self.window.ThreadFinished, self)
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title='Multi-threaded GUI')
self.threads = []
self.count = 0
panel = wx.Panel(self)
startBtn = wx.Button(panel, -1, 'Start a thread')
stopBtn = wx.Button(panel, -1, 'Stop all threads')
self.tc = wx.StaticText(panel, -1, 'Worker Threads: 00')
self.log = wx.TextCtrl(panel, -1, '',style=wx.TE_RICH|wx.TE_MULTILINE)
inner = wx.BoxSizer(wx.HORIZONTAL)
inner.Add(startBtn, 0, wx.RIGHT, 15)
inner.Add(stopBtn, 0, wx.RIGHT, 15)
inner.Add(self.tc, 0, wx.ALIGN_CENTER_VERTICAL)
main = wx.BoxSizer(wx.VERTICAL)
main.Add(inner, 0, wx.ALL, 5)
main.Add(self.log, 1, wx.EXPAND|wx.ALL, 5)
panel.SetSizer(main)
self.Bind(wx.EVT_BUTTON, self.OnStartButton, startBtn)
self.Bind(wx.EVT_BUTTON, self.OnStopButton, stopBtn)
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
self.UpdateCount()
def OnStartButton(self, evt):
self.count += 1
thread = WorkerThread(self.count, self)#创建一个线程
self.threads.append(thread)
self.UpdateCount()
thread.start()#启动线程
def OnStopButton(self, evt):
self.StopThreads()
self.UpdateCount()
def OnCloseWindow(self, evt):
self.StopThreads()
self.Destroy()
def StopThreads(self):#从池中删除线程 while self.threads:
thread = self.threads[0]
thread.stop()
self.threads.remove(thread)
def UpdateCount(self):
self.tc.SetLabel('Worker Threads: %d' % len(self.threads))
def LogMessage(self, msg):#注册一个消息
self.log.AppendText(msg)
def ThreadFinished(self, thread):#删除线程
self.threads.remove(thread)
self.UpdateCount()
app = wx.PySimpleApp()
frm = MyFrame()
frm.Show()
app.MainLoop()
下边是运行结果截图:
- 在wxPython中使用多线程
- 在wxPython中使用VTK
- 在wxPython中使用OpenGL
- wxPython中多线程处理的研究
- wxPython实现窗口在任务栏中闪烁
- 在Ubuntu系统中安装wxpython
- wxPython之在GridBagSizer中加入图片
- 在Mac系统上安装使用wxPython
- 在IOS中使用多线程
- Map在多线程中使用
- 队列在多线程中使用
- wxPython中使用剪贴板存取文本数据
- wxpython 中wxOGL的使用 第一篇
- wxpython 中wxOGL的使用 第二篇
- wxpython 中wxOGL的使用 第三篇
- wxPython中Icon, MDI, HtmlWindow使用示例
- wxPython中list的简单使用
- wxPython中tree控件的简单使用
- python中的多线程
- 如何用python实现一个多线程定时器
- cocos2d-x中lua与C++,C语言互相调用
- python写入mysql中文乱码问题
- 使用python多线程连接mysql数据库
- 在wxPython中使用多线程
- mysql插入数据不成功
- MYSQL获取自增ID的四种方法(转)
- python解决address already in use
- MySql数据库编程
- esp8266开发板WeMos的arduino开发环境搭建
- MySQL存储引擎(转)
- 实际经历告诉你,写一本技术书能赚多少钱(转)
- 剑指offer——面试题34:丑数