python - wxPython,实时捕获子进程的输出

标签 python multithreading wxpython subprocess

我正在开发 wxPython 中的应用程序,它是命令行实用程序的 GUI。 GUI 中有一个文本控件,用于显示应用程序的输出。我正在使用子进程启动 shell 命令,但在它完成之前我没有得到任何输出。

我尝试了几种解决方案,但似乎都不起作用。以下是我目前使用的代码(已更新):

       def onOk(self,event):        
        self.getControl('infotxt').Clear()
        try:
            thread = threading.Thread(target=self.run)
            thread.setDaemon(True)
            thread.start()

        except Exception:
            print 'Error starting thread'    

    def run(self):    
        args = dict()
        # creating a command to execute...

        cmd = ["aplcorr", "-vvfile", args['vvfile'], "-navfile", args['navfile'], "-lev1file", args['lev1file'], "-dem", args['dem'], "-igmfile", args['outfile']]

        proc = subprocess.Popen(' '.join(cmd), shell=True, stdout=subprocess.PIPE, stderr.subprocess.PIPE)         

        print
        while True:
            line = proc.stdout.readline()
            wx.Yield()
            if line.strip() == "":
                pass
            else:                
                print line.strip()                
            if not line: break
        proc.wait()

class RedirectInfoText:
    """ Class to redirect stdout text """
    def __init__(self,wxTextCtrl):
        self.out=wxTextCtrl

    def write(self,string):
        self.out.WriteText(string)

class RedirectErrorText:
    """ Class to redirect stderr text """    
    def __init__(self,wxTextCtrl):
        self.out.SetDefailtStyle(wx.TextAttr())
        self.out=wxTextCtrl

    def write(self,string):
        self.out.SetDefaultStyle(wx.TextAttr(wx.RED))
        self.out.WriteText(string)

特别是,我需要实时输出来创建进度条。

编辑:我根据迈克·德里斯科尔的建议更改了我的代码。它有时似乎有效,但大多数时候我都会遇到以下错误之一:

(python:7698): Gtk-CRITICAL **: gtk_text_layout_real_invalidate: assertion `layout->wrap_loop_count == 0' failed

(python:7893): Gtk-WARNING **: Invalid text buffer iterator: either the iterator is uninitialized, or the characters/pixbufs/widgets in the buffer have been modified since the iterator was created. You must use marks, character numbers, or line numbers to preserve a position across buffer modifications. You can apply tags and insert marks without invalidating your iterators, but any mutation that affects 'indexable' buffer contents (contents that can be referred to by character offset) will invalidate all outstanding iterators Segmentation fault (core dumped)

有什么线索吗?

最佳答案

问题是因为您正在尝试 wx.Yield 并从运行进程的线程的上下文中更新输出小部件,而不是从 GUI 线程进行更新。

  1. 由于您是从线程运行该进程,因此不需要调用 wx.Yield,因为您没有阻塞 GUI 线程,因此任何挂起的 UI 事件都应该正常处理。

  2. 查看 wx.PyOnDemandOutputWindow 类,了解如何处理源自非 GUI 线程的打印或其他输出的示例。

关于python - wxPython,实时捕获子进程的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6677248/

相关文章:

Python,mysql.connector 错误 : No Result Set to Fetch From; cgitb shows correct value being passed to function

Python 就地交换

java - 我需要为聊天客户端分配多少个线程?

java - 锁定数组的单元格

python - 将日期时间插入sql

python - 在 wx.python 中使顶部窗口不可访问

python - 将多个wx.EVT_MENU绑定(bind)到同一个方法?

python 3 : generator for map

python - 如何确定图像中的 channel 数?

c++ - 当项目依赖于用旧 c++ 编译的 .so 库时,用 c++11 编译