python脚本调用其他脚本_如何在一个python脚本中控制/调用另一个python脚本?(在脚本之间通信)...

该示例展示了如何在Python中使用wxPython库创建GUI,并结合threading模块实现多线程。工人线程(WorkerThread)继承自Thread类,负责执行耗时任务,同时使用wx.lib.pubsub的发布/订阅(pubsub)机制来与GUI进行通信。当工作线程完成任务时,它会向GUI发送消息,更新文本控件显示任务状态。用户可以通过按钮启动和停止线程,实现异步操作。
摘要由CSDN通过智能技术生成

我建议你看看threading module。通过子类化Thread类,可以为时间密集型作业创建新线程。在

那么对于线程之间的通信,您可以使用pubsub或{a3},我没有尝试过后者,因此我无法对其进行评论,但我推荐pubsub,因为它的易用性和wxpython的一部分是额外的。在

Here是一个关于运行长任务的wxpythonwiki页面,如果您想要最简单的线程用法示例,请跳到末尾。在

下面是一个简单的(可运行的)示例,说明如何使用pubsub将消息从您的workerThread发送到GUI。在import time

import wx

from threading import Thread

from wx.lib.pubsub import Publisher

class WorkerThread(Thread):

def __init__(self):

Thread.__init__(self)

#A flag that can be set

#to tell the thread to end

self.stop_flag = False

#This calls the run() to start the new thread

self.start()

def run(self):

""" Over-rides the super-classes run()"""

#Put everything in here that

#you want to run in your new thread

#e.g...

for x in range(20):

if self.stop_flag:

break

time.sleep(1)

#Broadcast a message to who ever's listening

Publisher.sendMessage("your_topic_name", x)

Publisher.sendMessage("your_topic_name", "finished")

def stop(self):

"""

Call this method to tell the thread to stop

"""

self.stop_flag = True

class GUI(wx.Frame):

def __init__(self, parent, id=-1,title=""):

wx.Frame.__init__(self, parent, id, title, size=(140,180))

self.SetMinSize((140,180))

panel = wx.Panel(id=wx.ID_ANY, name=u'mainPanel', parent=self)

#Subscribe to messages from the workerThread

Publisher().subscribe(self.your_message_handler, "your_topic_name")

#A button to start the workerThread

self.startButton = wx.Button(panel, wx.ID_ANY, 'Start thread')

self.Bind(wx.EVT_BUTTON, self.onStart, self.startButton)

#A button to stop the workerThread

self.stopButton = wx.Button(panel, wx.ID_ANY, 'Stop thread')

self.Bind(wx.EVT_BUTTON, self.onStop, self.stopButton)

#A text control to display messages from the worker thread

self.threadMessage = wx.TextCtrl(panel, wx.ID_ANY, '', size=(75, 20))

#Do the layout

sizer = wx.BoxSizer(wx.VERTICAL)

sizer.Add(self.startButton, 0, wx.ALL, 10)

sizer.Add(self.stopButton, 0, wx.ALL, 10)

sizer.Add(self.threadMessage, 0, wx.ALL, 10)

panel.SetSizerAndFit(sizer)

def onStart(self, event):

#Start the worker thread

self.worker = WorkerThread()

#Disable any widgets which could affect your thread

self.startButton.Disable()

def onStop(self, message):

self.worker.stop()

def your_message_handler(self, message):

message_data = message.data

if message_data == 'finished':

self.startButton.Enable()

self.threadMessage.SetLabel(str(message_data))

else:

self.threadMessage.SetLabel(str(message_data))

if __name__ == "__main__":

app = wx.PySimpleApp()

frame = GUI(None, wx.ID_ANY, 'Threading Example')

frame.Show()

app.MainLoop()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值