【问题标题】:Print output produced by logging.info in a wxpython text control在 wxpython 文本控件中打印由 logging.info 生成的输出
【发布时间】:2018-05-01 22:47:57
【问题描述】:

基本上我想要做的是将命令行可执行文件(如 coursera-dl)生成的输出放入文本控件中。但是它(命令行可执行文件)使用 logging.info 打印输出,并且似乎子进程无法读取 logging.info 打印输出,但是当 logging.info 函数更改为打印时,wxpython 能够从cmd 变成一个 textctrl。我使用python27。我的代码来自网络上的一堆代码:

self.courses_list = ""
def execute(self,command,textctrl):
        #clear the textctrl
        #try:
        textctrl.SetValue("")
        si=subprocess.STARTUPINFO()
        si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
        process = subprocess.Popen(command,stdout=subprocess.PIPE,**self.subprocess_args(False))
        output = ''
        self.out=[]
   # Poll process for new output until finished
    for line in iter(process.stdout.readline, ""):
        textctrl.AppendText(line)
        output += line#.strip().decode('utf-8')
        self.courses_list+=line
        print(line)
        self.out.append(line)

    process.wait()
    exitCode = process.returncode

    if (exitCode == 0):
        return output
    else:
        raise Exception(command, exitCode, output)

def subprocess_args(self,include_stdout=True):
    if hasattr(subprocess, 'STARTUPINFO'):
        si=subprocess.STARTUPINFO()
        si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
        env = os.environ
    else:
        si=None
        env=None

    if include_stdout:
        ret={'stdout:':subprocess.PIPE}
    else:
        ret={}

    ret.update({'stdin':subprocess.PIPE,
                'stderr':subprocess.PIPE,
                'startupinfo':si,
                'env':env})
    return ret

【问题讨论】:

  • 您是否尝试过从 stderr 而不是 stdout 读取子进程输出?据我所知,这些是应用程序可用的仅有的两个输出流。如果数据不在一个中,则它必须在另一个中。
  • @保罗·科尼利厄斯。我有。似乎它与记录不打印到标准输出的输出有关。所以我需要弄清楚的是如何让它打印到标准输出。但是我真的不想修改命令行源码

标签: python logging subprocess wxpython


【解决方案1】:

它似乎与从 stderr 读取输出有关。因此将 'stderr':subprocess.PIPE 更改为 'stderr':subprocess.STDOUT 解决了问题。所以这里是 ret 部分的变化是这样的

    ret.update({'stdin':subprocess.PIPE,
            'stderr':subprocess.STDOUT,#here's the solution
            'startupinfo':si,
            'env':env})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 2020-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多