简体   繁体   English

Wx.Python非阻塞GUI启动和停止脚本/模块

[英]Wx.Python non-Blocking GUI starting and stopping script/module

I want to be able to run and stop script/module from my GUI without blocking it. 我希望能够从我的GUI运行和停止脚本/模块,而不会阻止它。 I learned some basic things about threading and running "simple" long tasks inside GUI code. 我了解了一些有关在GUI代码中线程化和运行“简单”长任务的基本知识。 However all the examples are about simple while or for loop that can bi aborted. 但是,所有示例均涉及简单的whilefor循环,可能会导致异常终止。 In most cases it is some kind of counting. 在大多数情况下,这是一种计数。

So the question is: How to run/stop external script/module with wx.python based GUI? 所以问题是:如何使用基于wx.python的GUI运行/停止外部脚本/模块? The script is nothing specific it can be any kind of long task. 该脚本没有什么特别的,可以是任何长任务。

Here is the basic wx.Python example code! 这是基本的wx.Python示例代码!

import wx

class MyApp (wx.App):
    def OnInit(self):
        self.frame = MyFrame(None, title = 'Example')
        self.SetTopWindow(self.frame)
        self.frame.Show()

        return True

class MyFrame(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title ,style = (wx.MINIMIZE_BOX | wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.CLIP_CHILDREN))

        button1 = wx.Button(self,-1,'Start')
        button2 = wx.Button(self,-1, 'Stop')
        self.gauge1 = wx.Button(self, -1)

        box = wx.BoxSizer(wx.VERTICAL)

        box.Add(button1,1, wx.ALL, 10)
        box.Add(button2,1, wx.ALL, 10)
        box.Add(self.gauge1,1,wx.EXPAND|wx.ALL,10)

        self.SetSizer(box, wx.EXPAND)

        self.Bind(wx.EVT_BUTTON, self.OnStart, button1)
        self.Bind(wx.EVT_BUTTON, self.OnStop, button2)

    def OnStart(self,e):
        pass


    def OnStop(self,e):
        pass


if __name__=='__main__':
    app = MyApp(False)
    app.MainLoop()

The script that you are calling will need some mechanism for quitting cleanly or you will just have to deal with the mess. 您正在调用的脚本将需要某种机制来彻底退出,否则您将只需要处理混乱。 I would use Python's subprocess module to launch the external script and then use something like psutil ( https://github.com/giampaolo/psutil ) to kill it if I needed to. 如果需要,我可以使用Python的子进程模块启动外部脚本,然后使用psutil( https://github.com/giampaolo/psutil )之类的东西将其杀死。 That would require you to grab the process id (pid) with subprocess and keep track of it so you could kill it later. 这将要求您使用子进程来获取进程ID(pid)并对其进行跟踪,以便稍后将其杀死。

I wrote up an example of using psutil in wxPython here: http://www.blog.pythonlibrary.org/2012/07/13/wxpython-creating-your-own-cross-platform-process-monitor-with-psutil/ 我在这里写了一个在wxPython中使用psutil的示例: http : //www.blog.pythonlibrary.org/2012/07/13/wxpython-creating-your-own-cross-platform-process-monitor-with-psutil/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM