How can I get the default colors in GTK?

后端 未结 2 1380
故里飘歌
故里飘歌 2021-02-20 00:06

Context

In GTK 3, people can set their own themes. Even the default theme (Adwaita) is provided with two variants: a light one and a dark one. As I am writing my own w

2条回答
  •  温柔的废话
    2021-02-20 01:05

    You don't need to instantiate GTK widgets to retrieve their StyleContext.

    You can create an empty Gtk.StyleContext and set the Gtk.WidgetPath of a widget class.

    The foreground color can be retrieved with .get_color(). Other colors and style properties can be retrieved with .get_property().

    Both methods need Gtk.StateFlags.

    For properties, see GTK+ CSS Overview and GTK+ CSS Properties.

    import gi
    gi.require_version('Gtk', '3.0')
    from gi.repository import Gtk
    
    # Create an empty style context
    style_ctx = Gtk.StyleContext();
    
    # Create an empty widget path
    widget_path =  Gtk.WidgetPath();
    
    # Specify the widget class type you want to get colors from
    widget_path.append_type(Gtk.Button);
    style_ctx.set_path(widget_path);
    
    # Print style context colors of widget class Gtk.Button
    print('Gtk.Button: Normal:')
    print('foreground color: ', style_ctx.get_color(Gtk.StateFlags.NORMAL) )
    print('color:            ', style_ctx.get_property('color', Gtk.StateFlags.NORMAL) )
    print('background color: ', style_ctx.get_property('background-color', Gtk.StateFlags.NORMAL) )
    print('outline color:    ', style_ctx.get_property('outline-color', Gtk.StateFlags.NORMAL) )
    
    print('Gtk.Button: Link:')
    print('foreground color: ', style_ctx.get_color(Gtk.StateFlags.LINK) )
    print('color:            ', style_ctx.get_property('color', Gtk.StateFlags.LINK) )
    print('background color: ', style_ctx.get_property('background-color', Gtk.StateFlags.LINK) )
    print('outline color:    ', style_ctx.get_property('outline-color', Gtk.StateFlags.LINK) )
    

提交回复
热议问题