我想添加虚拟python外壳与Vte到我的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()发布于 2019-03-14 16:37:41
有最终的工作解决方案,我犯了严重的错误,现在它工作得很好:)。感谢@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()发布于 2019-03-12 20:19:27
您似乎遗漏了两个参数: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)https://stackoverflow.com/questions/55105447
复制相似问题