6

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.)

TheDev
  • 407
  • 2
  • 12
lefthander
  • 163
  • 1
  • 4
  • Finally someone didn't incorrectly add the `pygtk` tag! Huzzah! – Aran-Fey Mar 10 '19 at 16:52
  • 1
    I can't test it myself (no gtk installed on this PC), but you can try replacing `GtkEntry ` with `entry`, as seen in Gtk's [css overview](https://developer.gnome.org/gtk3/stable/chap-css-overview.html#css-overview). – Aran-Fey Mar 10 '19 at 16:56
  • @Aran-Fey, that works! Thanks for the answer and the resource. If you write it up as a proper answer, I'll approve it. – lefthander Mar 10 '19 at 18:02
  • You probably found an old tutorial where GtkEntry was still used. – shevy Feb 28 '21 at 16:02

1 Answers1

5

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

entry {
    background: yellow;
}
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149