Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
运行命令行程序的wxpythongui_Python_Wxpython_Subprocess - Fatal编程技术网

运行命令行程序的wxpythongui

运行命令行程序的wxpythongui,python,wxpython,subprocess,Python,Wxpython,Subprocess,我已经搜索了一个小时,但找不到这个问题的确切答案 我正在尝试编写一个wxpythongui应用程序,它有一个启动命令行工具的按钮(全部在Windows上)。该工具运行大约需要5分钟,并在运行过程中生成输出 我希望GUI具有某种文本窗口,在输出发生时显示输出。我还想终止GUI以终止命令行进程 我已经研究了线程和Popen,但似乎无法找出它们之间的正确连接来实现这一点。有人能给我举个合理的例子吗?我写了一篇文章,在文章中,我按照你所说的思路做了一些事情。我需要运行ping和traceroute并实时

我已经搜索了一个小时,但找不到这个问题的确切答案

我正在尝试编写一个wxpythongui应用程序,它有一个启动命令行工具的按钮(全部在Windows上)。该工具运行大约需要5分钟,并在运行过程中生成输出

我希望GUI具有某种文本窗口,在输出发生时显示输出。我还想终止GUI以终止命令行进程


我已经研究了线程和Popen,但似乎无法找出它们之间的正确连接来实现这一点。有人能给我举个合理的例子吗?

我写了一篇文章,在文章中,我按照你所说的思路做了一些事情。我需要运行ping和traceroute并实时捕获它们的输出。以下是文章:

基本上,您需要将stdout重定向到文本控件,然后执行如下操作:

proc = subprocess.Popen("ping %s" % ip, shell=True, 
                            stdout=subprocess.PIPE) 
line = proc.stdout.readline()
print line.strip()

如您所见,我使用子流程启动ping并读取其标准输出。然后,我使用strip()命令在打印前删除行的开头和结尾的额外空白。当你打印时,它会被重定向到文本控件。

我写了一篇文章,在文章中我按照你所说的思路做了一些事情。我需要运行ping和traceroute并实时捕获它们的输出。以下是文章:

基本上,您需要将stdout重定向到文本控件,然后执行如下操作:

proc = subprocess.Popen("ping %s" % ip, shell=True, 
                            stdout=subprocess.PIPE) 
line = proc.stdout.readline()
print line.strip()

如您所见,我使用子流程启动ping并读取其标准输出。然后,我使用strip()命令在打印前删除行的开头和结尾的额外空白。当你打印时,它会被重定向到文本控件。

我在我的应用程序中的wxPython中做了同样的事情。它运行pyInstaller命令,并在textctrl中逐行捕获输出

在主应用程序框架中,有一个按钮可以调用Submit上的

def OnSubmit(self, e):
    ...
     # this is just a list of what to run on the command line, something like [python, pyinstaller.py, myscript.py, --someflag, --someother flag]
    flags = util.getflags(self.fbb.GetValue())
    for line in self.CallInstaller(flags): # generator function that yields a line
        self.txtresults.AppendText(line) # which is output to the txtresults widget here
CallInstaller
执行命令的实际运行,生成一行并运行wx.Yield(),这样屏幕不会冻结得太厉害。你可以把它移到它自己的线程中,但我没有费心

def CallInstaller(self, flags):
        # simple subprocess.Popen call, outputs both stdout and stderr to pipe
        p = subprocess.Popen(flags, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        while(True): 
            retcode = p.poll() # waits for a return code, until we get one..
            line = p.stdout.readline() # we get any output
            wx.Yield() # we give the GUI a chance to breathe
            yield line # and we yield a line
            if(retcode is not None): # if we get a retcode, the loop ends, hooray!
                yield ("Pyinstaller returned return code: {}".format(retcode))
                break

我在我的应用程序中的wxPython中做了同样的事情。它运行pyInstaller命令,并在textctrl中逐行捕获输出

在主应用程序框架中,有一个按钮可以调用Submit上的

def OnSubmit(self, e):
    ...
     # this is just a list of what to run on the command line, something like [python, pyinstaller.py, myscript.py, --someflag, --someother flag]
    flags = util.getflags(self.fbb.GetValue())
    for line in self.CallInstaller(flags): # generator function that yields a line
        self.txtresults.AppendText(line) # which is output to the txtresults widget here
CallInstaller
执行命令的实际运行,生成一行并运行wx.Yield(),这样屏幕不会冻结得太厉害。你可以把它移到它自己的线程中,但我没有费心

def CallInstaller(self, flags):
        # simple subprocess.Popen call, outputs both stdout and stderr to pipe
        p = subprocess.Popen(flags, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        while(True): 
            retcode = p.poll() # waits for a return code, until we get one..
            line = p.stdout.readline() # we get any output
            wx.Yield() # we give the GUI a chance to breathe
            yield line # and we yield a line
            if(retcode is not None): # if we get a retcode, the loop ends, hooray!
                yield ("Pyinstaller returned return code: {}".format(retcode))
                break
下面是一些例子。