虚拟 python shell 与 Vte.Pty.spawn_async()

Virtual python shell with Vte.Pty.spawn_async()

我想将带有 Vte 的虚拟 python shell 添加到我的 GTK3.0 python3 应用程序和 I am able to do this with spawn_sync() 方法中,但是这种方法已被弃用,所以我想用 Vte.Pty.spawn_async() 的首选方式来做,但我不明白如何....我尝试了部分代码,但没有运气。请给我一些 python 中的工作代码。例如,我试过这样的变体:

import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Vte', '2.91')
from gi.repository import Gtk, Vte

    class TheWindow(Gtk.Window):
        def __init__(self):
            Gtk.Window.__init__(self, title="GTK3 IDE")
            self.set_default_size(600, 300)

            self.terminal = Vte.Terminal()
            self.pty = self.terminal.pty_new_sync(Vte.PtyFlags.DEFAULT)
            #self.pty.child_setup()
            #self.pty = Vte.Pty(fd=-1)
            #self.pty.new_sync(Vte.PtyFlags.DEFAULT, None)

            self.pty.spawn_async(
                None,
                ["/bin/python"],
                None,
                GLib.SpawnFlags.DO_NOT_REAP_CHILD,
                None,
                None,
                -1,
                None,
                self.ready
                )

        def ready(self, pty, task):
            print('pty ', pty)

            self.terminal.set_pty(self.pty)

            box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)

            scroller = Gtk.ScrolledWindow()
            scroller.set_hexpand(True)
            scroller.set_vexpand(True)
            scroller.add(self.terminal)
            box.pack_start(scroller, False, True, 2)
            self.add(box)

    win=TheWindow()
    win.connect('destroy', Gtk.main_quit)
    win.show_all()
    Gtk.main()

您似乎缺少 2 个参数:Gio.CancellableGio.AsyncReadyCallbackdocumentation.

中提到了它们

您需要回调来了解异步调用何时完成。

class TheWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="GTK3 IDE")

        self.box = Gtk.HBox(spacing=6)
        self.add(self.box)

        self.terminal = Vte.Terminal()
        self.terminal.pty_new_sync(Vte.PtyFlags.DEFAULT)

        self.pty = Vte.Pty()
        self.pty.new_sync(Vte.PtyFlags.DEFAULT, None)
        self.pty.spawn_async(
            None,
            ["/bin/python"],
            None,
            GLib.SpawnFlags.DO_NOT_REAP_CHILD,
            None,
            None,
            -1,
            None,
            self.ready
            )

    def ready(self, pty, task):
        print('pty ', pty)

有最终的解决方案,我犯了严重的错误,现在效果很好:)。感谢@elya5 的回答:).

import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Vte', '2.91')
from gi.repository import Gtk, Vte, GLib, Pango, Gio

    class TheWindow(Gtk.Window):
        def __init__(self):
            Gtk.Window.__init__(self, title="GTK3 IDE")
            self.set_default_size(600, 300)

            terminal = Vte.Terminal()
            #pty = terminal.pty_new_sync(Vte.PtyFlags.DEFAULT)
            pty = Vte.Pty.new_sync(Vte.PtyFlags.DEFAULT)
            terminal.set_pty(pty)

            pty.spawn_async(
                None,
                ["/bin/python"],
                None,
                GLib.SpawnFlags.DO_NOT_REAP_CHILD,
                None,
                None,
                -1,
                None,
                self.ready
                )

            #self.terminal.get_pty(self.pty)

            box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)

            scroller = Gtk.ScrolledWindow()
            scroller.set_hexpand(True)
            scroller.set_vexpand(True)
            scroller.add(terminal)
            box.pack_start(scroller, False, True, 2)
            self.add(box)

        def ready(self, pty, task):
            print('pty ', pty)


    win=TheWindow()
    win.connect('destroy', Gtk.main_quit)
    win.show_all()
    Gtk.main()