我想在我的GTK3.0 python3应用程序和I am able to do this with spawn_sync()方法中添加带有Vte的虚拟python shell,但是这个方法已被弃用,所以我想用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()
好像你错过了两个论点:Gio.Cancellable
和Gio.AsyncReadyCallback
。它们在documentation中被提及。
您需要回调才能知道异步调用何时完成。
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()