Python,快速和Glade,在文本视图中显示stdout

Python,快速和Glade,在文本视图中显示stdout,python,ubuntu,gtk,textview,subprocess,Python,Ubuntu,Gtk,Textview,Subprocess,我花了很长时间寻找一种方法来做到这一点,但到目前为止,我什么也没想到( 我正在尝试为我制作的一个小CLI程序制作GUI,所以我认为使用Ubuntu的“快速”将是最简单的方法。基本上,它似乎使用Glade来制作GUI。我知道我需要在子进程中运行CLI后端,然后将stdout和stderr发送到textview。但我不知道如何做到这一点 这是Glade/Quick为我希望输出显示在其中的对话框创建的代码: from gi.repository import Gtk # pylint: disable

我花了很长时间寻找一种方法来做到这一点,但到目前为止,我什么也没想到(

我正在尝试为我制作的一个小CLI程序制作GUI,所以我认为使用Ubuntu的“快速”将是最简单的方法。基本上,它似乎使用Glade来制作GUI。我知道我需要在子进程中运行CLI后端,然后将stdout和stderr发送到textview。但我不知道如何做到这一点

这是Glade/Quick为我希望输出显示在其中的对话框创建的代码:

from gi.repository import Gtk # pylint: disable=E0611

from onice_lib.helpers import get_builder

import gettext
from gettext import gettext as _
gettext.textdomain('onice')

class BackupDialog(Gtk.Dialog):
    __gtype_name__ = "BackupDialog"

    def __new__(cls):
        """Special static method that's automatically called by Python when 
        constructing a new instance of this class.

        Returns a fully instantiated BackupDialog object.
        """
        builder = get_builder('BackupDialog')
        new_object = builder.get_object('backup_dialog')
        new_object.finish_initializing(builder)
        return new_object

    def finish_initializing(self, builder):
        """Called when we're finished initializing.

        finish_initalizing should be called after parsing the ui definition
        and creating a BackupDialog object with it in order to
        finish initializing the start of the new BackupDialog
        instance.
        """
        # Get a reference to the builder and set up the signals.
        self.builder = builder
        self.ui = builder.get_ui(self)

        self.test = False

    def on_btn_cancel_now_clicked(self, widget, data=None):
        # TODO: Send SIGTERM to the subprocess
        self.destroy()

if __name__ == "__main__":
    dialog = BackupDialog()
    dialog.show()
    Gtk.main()
如果我把它放在finish_初始化函数中

backend_process = subprocess.Popen(["python", <path to backend>], stdout=subprocess.PIPE, shell=False)
但我只需要知道如何在每次有新的stdout时调用它

但我只需要知道如何在每次有新的stdout时调用它

您可以使用
GObject.io\u add\u watch
监视子流程的输出,或者创建一个单独的线程来读取子流程

# read from subprocess
def read_data(source, condition):
    line = source.readline() # might block
    if not line:
        source.close()
        return False # stop reading
    # update text
    label.set_text('Subprocess output: %r' % (line.strip(),))
    return True # continue reading
io_id = GObject.io_add_watch(proc.stdout, GObject.IO_IN, read_data)
或者使用线程:

# read from subprocess in a separate thread
def reader_thread(proc, update_text):
    with closing(proc.stdout) as file:
        for line in iter(file.readline, b''):
            # execute update_text() in GUI thread
            GObject.idle_add(update_text, 'Subprocess output: %r' % (
                    line.strip(),))

t = Thread(target=reader_thread, args=[proc, label.set_text])
t.daemon = True # exit with the program
t.start()

# read from subprocess in a separate thread
def reader_thread(proc, update_text):
    with closing(proc.stdout) as file:
        for line in iter(file.readline, b''):
            # execute update_text() in GUI thread
            GObject.idle_add(update_text, 'Subprocess output: %r' % (
                    line.strip(),))

t = Thread(target=reader_thread, args=[proc, label.set_text])
t.daemon = True # exit with the program
t.start()