pythongtk3pygobjectgtkentry

Styling all GtkEntry widgets with CSS using Python3 PyGObject


I'm developing a GUI with PyGObject and am trying to style all the entry widgets. From this post I get the impression that I should be able to create a CSS style that would apply to all GtkEntry objects, but for some reason it's not working for me in the code below:

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

class CompletionApp(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)

        screen = Gdk.Screen.get_default()
        provider = Gtk.CssProvider()
        style_context = Gtk.StyleContext()
        style_context.add_provider_for_screen(
            screen, provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
        )
        css = b"""
        GtkEntry {
            background: yellow;
        }
        """
        provider.load_from_data(css)

        self.entry = Gtk.Entry()
        self.entry.set_name("myentry")
        self.add(self.entry)
        self.connect("destroy", Gtk.main_quit)
        self.show_all()
        Gtk.main()

if __name__ == '__main__':
    win = CompletionApp()

If I change "GtkEntry" to "#myentry" in the CSS, it works as expected. Can anyone help me understand what I should be doing to style all entry widgets? (For the sake of brevity, the example above only has one entry, but I'm working on an application that has several entries.)


Solution

  • As can be seen in the Gtk+ CSS overview, widget names should be lower case and not start with "Gtk":

    entry {
        background: yellow;
    }