python-2.7terminalgtkfont-sizevte

Using python, GTK+3, Vte libs. How to change the font size of a terminal?


I made a program which shows four terminals in one window, but the font size is way too big. Here is my code:

#!/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()

According to the documentation Vte set_font I used:

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

And I get the following error:

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

The example is from a stack overflow question: how to enable transparency in vte.Terminal

Then I used, according to this Vte 'decrease-font-size' signal :

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

Nothing happens with this one. And the rest also don't work. I also noticed that I can't really change the size of one terminal using:

terminal1.set_size(30, 29)

Seems to change nothing.

For these examples I used for the sake of simplicity only terminal1. Also one weird thing is that, when I do the "clear" command, it seems to rearrange itself and the font size becomes bigger. I apologize for weird English. It's not my native language, but I'll try my best. When it comes to programming, I have only experience with python and associated libraries or modules. I'm using a Linux Debian version as OS.

Greets


Solution

  • Connect to the realize signal of the Vte.Terminal as only then the Pango.FontDescription() is created, then get_font() on the terminal and set_size() on the Pango.FontDescription() returned by that:

    #!/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()