Another user here on StackOverflow has some problems styling his Gtk application with CSS. I found a solution for part of the problems, but for one I don't know anything. The original post is with C code, but the following Python Minimal Reproducible Example has the same problem:
#!/usr/bin/python3
#-*-coding: utf-8-*-
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
win = Gtk.Window()
win.connect("destroy", lambda _: Gtk.main_quit())
box = Gtk.VBox()
win.add(box)
button = Gtk.Button("Button")
prov = Gtk.CssProvider.new()
prov.load_from_data("""
button {
color: red;
background-color: green;
margin: 30px;
}
""".encode())
ctx = win.get_style_context()
ctx.add_provider(prov, 1000)
box.add(button)
win.show_all()
Gtk.main()
The problem is, the margins are not showing, nor the font and background color, whereas they are correctly shown in this example:
#!/usr/bin/python3
#-*-coding: utf-8-*-
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
win = Gtk.Window()
win.connect("destroy", lambda _: Gtk.main_quit())
box = Gtk.VBox()
win.add(box)
button = Gtk.Button("Button")
prov = Gtk.CssProvider.new()
prov.load_from_data("""
* {
color: red;
background-color: green;
margin: 30px;
}
""".encode())
ctx = button.get_style_context()
ctx.add_provider(prov, 1000)
box.add(button)
win.show_all()
Gtk.main()
This led me to the conclusion that the selector button somehow does not do what I want and expect. Why is the selector wrong, and which would be the right selector to select the button ?
#define GTK_STYLE_PROVIDER_PRIORITY_USER 800 You should not use priorities higher than this, to give the user the last word.Maybe your GTK theme overrides your style? Have you tried it with adwaita?