首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python,Glade和Glade,在TextView中显示stdout

Python,Glade和Glade,在TextView中显示stdout
EN

Stack Overflow用户
提问于 2013-03-11 02:48:14
回答 1查看 692关注 0票数 1

我花了很长时间想办法来做这件事,但到目前为止我什么都没想出来。:(

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

这是Glade/快速为对话框创建的代码,我希望将输出显示到该对话框中:

代码语言:javascript
运行
复制
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_initializing函数中

代码语言:javascript
运行
复制
backend_process = subprocess.Popen(["python", <path to backend>], stdout=subprocess.PIPE, shell=False)

然后进程作为另一个PID启动并运行,这正是我想要的,但是现在我如何将backend_process.stdout发送到TextView呢?我可以用以下方式写入文本视图:

代码语言:javascript
运行
复制
BackupDialog.ui.backup_output.get_buffer().insert_at_cursor("TEXT")

但我只需要知道,每当有新的标准输出时,如何调用它。

EN

回答 1

Stack Overflow用户

发布于 2013-03-13 03:05:00

但我只需要知道,每当有新的标准输出时,如何调用它。

您可以使用GObject.io_add_watch来监视子进程输出,或者创建一个单独的线程从子进程中读取。

代码语言:javascript
运行
复制
# 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)

或者使用线程:

代码语言:javascript
运行
复制
# 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()

完整代码示例

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15330419

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档