Python - 如何将文本写入GTK + Vte终端小部件?

时间:2018-02-28 10:12:52

标签: python python-3.x gtk3

我正在尝试使用Vte终端小部件来显示我的python脚本中的文本。

我没有设置工作目录或模拟器,我只想要一个处理文本和ansi转义序列的空终端

我有这段代码,它适用于python 2.7:

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

from gi.repository import Gtk, Vte
from gi.repository import GLib
import os

terminal     = Vte.Terminal()
terminal.spawn_sync(
    Vte.PtyFlags.DEFAULT,
    None,
    [],
    [],
    GLib.SpawnFlags.DO_NOT_REAP_CHILD,
    None,
    None,
    )

win = Gtk.Window()
win.connect('delete-event', Gtk.main_quit)
win.add(terminal)
win.show_all()

terminal.feed('hello') #string to display

Gtk.main()

但它在python 3.5中不起作用,我得到的只是一个空白终端。

2 个答案:

答案 0 :(得分:1)

以下是显示文字的正确方法:

import gi
gi.require_version('Gtk', '3.0')

from gi.repository import Gtk, Gdk, GLib
import os

textview = Gtk.TextView()
textview.set_name("TextView")
buf = Gtk.TextBuffer()
textview.set_buffer(buf)
buf.set_text("This is a test message\n")

style_provider = Gtk.CssProvider()

css = """
#TextView{
    background-color: black;
}
"""
style_provider.load_from_data(bytes(css.encode()))
    Gtk.StyleContext.add_provider_for_screen(
    Gdk.Screen.get_default(), style_provider,
    Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
    )

for color in ("red", "yellow", "green", "blue", "white"):
    buf.insert_markup(
        buf.get_end_iter(),
        '<span color="{:s}">This is a test message</span>\n'.format(color),
         -1)

win = Gtk.Window()
win.connect('delete-event', Gtk.main_quit)
win.add(textview)
win.show_all()


Gtk.main()

顺便说一下,这更容易。

修改:包含颜色。

答案 1 :(得分:0)

嗨,我希望这对我的迷你术语有所帮助,在python3.7下使用vte.Terminal可以实现。

此旧格式

terminal.spawn_sync

现已弃用。

因此,您可以使用较新的格式,如以下完整源代码所示 和Terminator3链接:https://gitlab.com/raffiki-python/terminator-py3/-/blob/master/miniTerm.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
#  miniTerm.py

# Importing the libraries
import os
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
gi.require_version('Vte', '2.91')  # vte-0.38 (gnome-3.4)
from gi.repository import Vte, GLib, GObject


class Terminal(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Hello Mini Terminal")
        self.set_default_size(600, 400)
        self.set_border_width(3)

        self.notebook = Gtk.Notebook()
        self.add(self.notebook)
        VTE_ADDITIONAL_WORDCHARS = "-,./?%&#:_";

        term1 = Vte.Terminal.new()
        term1.set_size(30, 3)
        term1.set_mouse_autohide(True)
        term1.set_scroll_on_output(True)
        term1.set_audible_bell(True)
        term1_pty = term1.pty_new_sync(Vte.PtyFlags.DEFAULT, None)
        term1.set_pty(term1_pty)
        term1.set_word_char_exceptions(VTE_ADDITIONAL_WORDCHARS)
        term1_fd = Vte.Pty.get_fd(term1_pty)
        term1.spawn_async(
                Vte.PtyFlags.DEFAULT, #default is fine
                os.environ['HOME'], #where to start the command?
                ["/bin/bash"], #where is the emulator?
                [""], #it's ok to leave this list empty
                GLib.SpawnFlags.DO_NOT_REAP_CHILD,
                None, -1, term1_fd)

        term2 = Vte.Terminal.new()
        term2.set_size(30, 3)
        term2.set_mouse_autohide(True)
        term2.set_scroll_on_output(True)
        term2_pty = term2.pty_new_sync(Vte.PtyFlags.DEFAULT, None)
        term2.set_pty(term2_pty)
        term2.set_word_char_exceptions(VTE_ADDITIONAL_WORDCHARS)
        term2_fd = Vte.Pty.get_fd(term2_pty)
        term2.spawn_async(
                Vte.PtyFlags.DEFAULT, #default is fine
                os.environ['HOME'], #where to start the command?
                ["/bin/bash"], #where is the emulator?
                [""], #it's ok to leave this list empty
                GLib.SpawnFlags.DO_NOT_REAP_CHILD,
                None, -1, term2_fd)

        scrollWin1 = Gtk.ScrolledWindow()
        scrollWin1.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
        scrollWin1.set_border_width(1)
        Gtk.Container.add(scrollWin1, term1)
        self.notebook.append_page(scrollWin1, Gtk.Label(label="Terminal 1"))

        scrollWin2 = Gtk.ScrolledWindow()
        scrollWin2.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
        scrollWin2.set_border_width(1)
        Gtk.Container.add(scrollWin2, term2)
        self.notebook.append_page(scrollWin2, Gtk.Label(label="Terminal 2"))


win = Terminal()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()