How do I apply a CSS to a clutter stage?


Tags: python,css,clutter

Problem :

I'm trying to get some clutter code to apply styles through CSS, but I can't get the styles applied. This is some example of the code that is not working:

#!/usr/bin/python
from gi.repository import Clutter, GdkPixbuf, Cogl, Gtk, Gdk

Clutter.init([])
stage = Clutter.Stage()
stage.set_size(600, 300)

pixbuf = GdkPixbuf.Pixbuf.new_from_file('menu.svg')
pixel_format = Cogl.PixelFormat.RGBA_8888 if pixbuf.get_has_alpha() \
    else Cogl.PixelFormat.RGB_888

image = Clutter.Image()
image.set_data( pixbuf.get_pixels(), pixel_format, pixbuf.get_width(),
    pixbuf.get_height(), pixbuf.get_rowstride())

image_actor = Clutter.Actor()
image_actor.set_content_scaling_filters(
    Clutter.ScalingFilter.TRILINEAR,
    Clutter.ScalingFilter.LINEAR
)
image_actor.set_content(image)
image_actor.set_size(22,22)
image_actor.set_name("MyImage")
stage.add_child(image_actor)

css = """
#MyImage {
    icon-shadow: red 0px 0px 30px;
    background-color: #000000;
    margin: 10px;
}
"""
style_provider = Gtk.CssProvider()
style_provider.load_from_data(css)

# This is the part that doesn't work
Gtk.StyleContext.add_provider_for_screen(
    Gdk.Screen.get_default(),
    style_provider,
    Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
)

stage.connect("destroy", lambda w:  Clutter.main_quit())
stage.show()
Clutter.main()

I've tried several different things, but the style never gets applied.



Solution :

Clutter does not know anything about GTK+ or CSS, so you cannot use the GTK+ CSS machinery with Clutter.

if you want to use a GtkStyleContext and the GTK+ API to draw something inside a ClutterActor, you can use a ClutterCanvas content, and then use the gtk_render_* API with the Cairo context that the ClutterCanvas::draw signal gives you.


    CSS Howto..

    How to line up a css ::before pseudo element like a grid

    How to align the overlay with the list of icons on hover- JS /CSS

    CSS: how to “plot” invisible text over an image?

    How do I create two columns using CSS?

    How to show CSS transitions only on hover?

    How to center content vertically with unknown content and fixed layout

    Only Javascript code to show a class without onclick

    How to convert Xpath to CSS

    How to embed SVG styling with javascript?

    How can I cut too wide image in HTML?

    How to specify BOTH max-width % and px [closed]

    How do I style the asp.net membership control with css?

    How to cut the bottom overflow of page CSS/HTML

    jQuery: How to change the size of a css block?

    Do East Asian (e.g. Chinese) characters have baselines? How do they align with English in CSS?

    How to convert css margin value to pixels with just plain javascript?

    How to read minified CSS?

    How to create dynamic responsive four columns with border style?

    Jquery: how to get menu to expand towards the right instead of the left

    How to Decrease Image Brightness in CSS

    How do I keep my background image at the bottom of the page?

    How can I make my footer stay at the bottom with a dynamically re-sizable form?

    How to remove “left” value bootstrap popover

    How can I set my unordered list to be in the middle not in-line (see image)?

    How do I go about targeting the literal “first-child” of a div, styling depending on element type

    How to remove or hide element using jquery or css

    How to get rid of column border of gridview using CSS

    How to overlap auto-scaling images with an auto-scaling background

    How to get an li to expand to fit thumbnail content?

    How to hide autofill safari icon in input field