【问题标题】:Using python, GTK+3, Vte libs. How to change the font size of a terminal?使用 python、GTK+3、Vte 库。如何更改终端的字体大小?
【发布时间】:2019-01-01 23:05:59
【问题描述】:

我做了一个程序,在一个窗口中显示四个终端,但是字体太大了。这是我的代码:

#!/usr/bin/python shells.py

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

class MainWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="shells")
        self.resize(1000, 1000)

        table = Gtk.Table(2, 2, True)
        self.add(table)

        palette = [Gdk.color_parse('red')] * 16
        terminal1 = Vte.Terminal()
        terminal2 = Vte.Terminal()
        terminal3 = Vte.Terminal()
        terminal4 = Vte.Terminal()

        scrolledwindow1 = Gtk.ScrolledWindow()
        scrolledwindow1.add(terminal1)

        scrolledwindow2 = Gtk.ScrolledWindow()
        scrolledwindow2.add(terminal2)

        scrolledwindow3 = Gtk.ScrolledWindow()
        scrolledwindow3.add(terminal3)

        scrolledwindow4 = Gtk.ScrolledWindow()
        scrolledwindow4.add(terminal4)

        terminal1.fork_command_full(Vte.PtyFlags.DEFAULT, os.environ['HOME'], ["/bin  /sh"], 
                                   [], GLib.SpawnFlags.DO_NOT_REAP_CHILD, None, None)
        terminal2.fork_command_full(Vte.PtyFlags.DEFAULT, os.environ['HOME'], ["/bin/sh"],
                                   [], GLib.SpawnFlags.DO_NOT_REAP_CHILD, None, None)
        terminal3.fork_command_full(Vte.PtyFlags.DEFAULT, os.environ['HOME'], ["/bin/sh"],
                                   [], GLib.SpawnFlags.DO_NOT_REAP_CHILD, None, None)
        terminal4.fork_command_full(Vte.PtyFlags.DEFAULT, os.environ['HOME'], ["/bin/sh"],
                                   [], GLib.SpawnFlags.DO_NOT_REAP_CHILD, None, None)

        terminal1.set_colors(Gdk.color_parse('red'), Gdk.color_parse('black'), palette)
        terminal2.set_colors(Gdk.color_parse('green'), Gdk.color_parse('black'), palette)
        terminal3.set_colors(Gdk.color_parse('yellow'), Gdk.color_parse('black'), palette)
        terminal4.set_colors(Gdk.color_parse('blue'), Gdk.color_parse('black'), palette)

        table.attach(scrolledwindow1, 0, 1, 0, 1)
        table.attach(scrolledwindow2, 1, 2, 0, 1)
        table.attach(scrolledwindow3, 0, 1, 1, 2)
        table.attach(scrolledwindow4, 1, 2, 1, 2)

win = MainWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

根据我使用的文档Vte set_font

import pango
font = pango.FontDescription()
terminal1.set_font(font)

我收到以下错误:

AttributeError: type object 'Context' has no attribute '__info__'

该示例来自堆栈溢出问题: how to enable transparency in vte.Terminal

然后我用了,照着这个Vte 'decrease-font-size' signal

terminal1.connect('decrease-font-size', function, data)
def function():
    print "press crtl -to decrease font-size"

这个没有任何反应。其余的也不起作用。 我还注意到,我无法真正改变一个终端的大小:

terminal1.set_size(30, 29)

似乎没有任何改变。

对于这些示例,为了简单起见,我只使用了终端 1。 还有一件奇怪的事情是,当我执行“清除”命令时,它似乎重新排列并且字体变大了。 我为奇怪的英语道歉。这不是我的母语,但我会尽力而为。在编程方面,我只有 python 和相关库或模块的经验。我正在使用 Linux Debian 版本作为操作系统。

问候

【问题讨论】:

    标签: python-2.7 terminal gtk font-size vte


    【解决方案1】:

    连接到Vte.Terminalrealize 信号,因为只有这样才会创建Pango.FontDescription(),然后在终端上创建get_font(),然后在返回的Pango.FontDescription() 上创建set_size()

    #!/usr/bin/env python3
    
    
    import os
    
    from gi.repository import GLib
    from gi.repository import Pango
    from gi.repository import Gdk
    from gi.repository import Gtk
    from gi.repository import Vte
    
    
    class MainWindow(Gtk.Window):
        def __init__(self):
            Gtk.Window.__init__(self, title="shells")
            self.resize(1000, 1000)
    
            table = Gtk.Table(2, 2, True)
            self.add(table)
    
            palette = [Gdk.color_parse('red')] * 16
            terminal1 = Vte.Terminal()
            terminal1.connect('realize', self.on_realize_terminal)
            terminal2 = Vte.Terminal()
            terminal2.connect('realize', self.on_realize_terminal)
            terminal3 = Vte.Terminal()
            terminal3.connect('realize', self.on_realize_terminal)
            terminal4 = Vte.Terminal()
            terminal4.connect('realize', self.on_realize_terminal)
    
            scrolledwindow1 = Gtk.ScrolledWindow()
            scrolledwindow1.add(terminal1)
    
            scrolledwindow2 = Gtk.ScrolledWindow()
            scrolledwindow2.add(terminal2)
    
            scrolledwindow3 = Gtk.ScrolledWindow()
            scrolledwindow3.add(terminal3)
    
            scrolledwindow4 = Gtk.ScrolledWindow()
            scrolledwindow4.add(terminal4)
    
            terminal1.fork_command_full(Vte.PtyFlags.DEFAULT, os.environ['HOME'], ["/bin/sh"], 
                                       [], GLib.SpawnFlags.DO_NOT_REAP_CHILD, None, None)
            terminal2.fork_command_full(Vte.PtyFlags.DEFAULT, os.environ['HOME'], ["/bin/sh"],
                                       [], GLib.SpawnFlags.DO_NOT_REAP_CHILD, None, None)
            terminal3.fork_command_full(Vte.PtyFlags.DEFAULT, os.environ['HOME'], ["/bin/sh"],
                                       [], GLib.SpawnFlags.DO_NOT_REAP_CHILD, None, None)
            terminal4.fork_command_full(Vte.PtyFlags.DEFAULT, os.environ['HOME'], ["/bin/sh"],
                                       [], GLib.SpawnFlags.DO_NOT_REAP_CHILD, None, None)
    
            terminal1.set_colors(Gdk.color_parse('red'), Gdk.color_parse('black'), palette)
            terminal2.set_colors(Gdk.color_parse('green'), Gdk.color_parse('black'), palette)
            terminal3.set_colors(Gdk.color_parse('yellow'), Gdk.color_parse('black'), palette)
            terminal4.set_colors(Gdk.color_parse('blue'), Gdk.color_parse('black'), palette)
    
            table.attach(scrolledwindow1, 0, 1, 0, 1)
            table.attach(scrolledwindow2, 1, 2, 0, 1)
            table.attach(scrolledwindow3, 0, 1, 1, 2)
            table.attach(scrolledwindow4, 1, 2, 1, 2)
    
        def on_realize_terminal(self, terminal):
            font = terminal.get_font()
            font.set_size(16 * Pango.SCALE)
    
    
    if __name__ == '__main__':
        win = MainWindow()
        win.connect("delete-event", Gtk.main_quit)
        win.show_all()
        Gtk.main()
    

    【讨论】:

    • 对不起,但它不起作用。它也适用于他的人的代码:stackoverflow.com/questions/11686510/… 而且他不需要实现信号。可能因为布局不好,四个航站楼同时出现,一切都搞砸了?
    猜你喜欢
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-23
    • 2013-05-20
    • 2015-02-19
    • 2013-10-22
    相关资源
    最近更新 更多