Change font size of Gtk.MenuItem in Python3

45 views Asked by At

I want to add a unicode character to a Gtk.MenuItem. The font size, however, is way too small. Especially in comparison to normal ASCII characters in the menu.

I tried to use CSS to change the font size, but nothing changes. Am I missing something?

#! /usr/bin/env python3

import gi
gi.require_version('Gtk', '3.0')
gi.require_version('AyatanaAppIndicator3', '0.1')
from gi.repository import Gtk
from gi.repository import AyatanaAppIndicator3


def build_menu():
    menu = Gtk.Menu()
    item_play = Gtk.MenuItem.new()
    
    # Set unicode smiley
    item_play.set_label("\u263A")

    # This does not do anything
    css = b".fs { font-size: 40px; }"
    css_provider = Gtk.CssProvider()
    css_provider.load_from_data(css)
    item_play_sc = item_play.get_style_context()
    item_play_sc.add_class("fs")
    item_play_sc.add_provider(css_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER)

    menu.append(item_play)
    
    # Quit menu
    menu.append(Gtk.SeparatorMenuItem())
    item_quit = Gtk.MenuItem.new_with_label("Quit")
    item_quit.connect("activate", Gtk.main_quit)
    menu.append(item_quit)
    
    menu.show_all()
    return menu

indicator = AyatanaAppIndicator3.Indicator.new("smile_indicator",
                                               "face-smile",
                                               AyatanaAppIndicator3.IndicatorCategory.OTHER)
indicator.set_status(AyatanaAppIndicator3.IndicatorStatus.ACTIVE)
indicator.set_menu(build_menu())
Gtk.main()

As an alternative, I tried to add an image. But now, I need to add both an image AND a label to show the image. If no label is passed to the function, the image is not shown. How can I force the image to show, even no label is given?

def menu_item(label="", icon=None, function=None, parameters=None):
    item = Gtk.MenuItem.new()
    item_box = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 6)

    if icon:
        if os.path.exists(icon):
            img = Gtk.Image.new_from_file(icon, Gtk.IconSize.MENU)
            print((f"Icon path added: '{icon}'"))
        else:
            img = Gtk.Image.new_from_icon_name(icon, Gtk.IconSize.MENU)
            print((f"Icon name added: '{icon}'"))
        item_box.pack_start(img, False, False, 0)
    if label:
        item_box.pack_start(Gtk.Label.new(label), False, False, 0)
        print((f"Label added: '{label}'"))

    item.add(item_box)
    item.show_all()

    if function and parameters:
        item.connect("activate", lambda * a: function(parameters))
    elif function:
        item.connect("activate", lambda * a: function())
    return item

So, this will not show anything:

menu.append(menu_item(icon="face-smile"))

But this will show both the image and the label:

menu.append(menu_item(label="Smile", icon="face-smile"))
0

There are 0 answers