18.5. Widget Styles

The following methods get and set the style associated with a widget:

  widget.set_style(style)

  style = widget.get_style()

The following function:

  style = get_default_style()

gets the default style.

A style contains the graphics information needed by a widget to draw itself in its various states:

  STATE_NORMAL        # The state during normal operation.
  STATE_ACTIVE        # The widget is currently active, such as a button pushed
  STATE_PRELIGHT      # The mouse pointer is over the widget.
  STATE_SELECTED      # The widget is selected
  STATE_INSENSITIVE   # The widget is disabled

A style contains the following attributes:

  fg          # a list of 5 foreground colors - one for each state
  bg          # a list of 5 background colors
  light       # a list of 5 colors - created during set_style() method
  dark        # a list of 5 colors - created during set_style() method
  mid         # a list of 5 colors - created during set_style() method
  text        # a list of 5 colors
  base        # a list of 5 colors
  text_aa     # a list of 5 colors halfway between text/base
  
  black       # the black color
  white       # the white color
  font_desc   # the default pango font description

  xthickness  #
  ythickness  #

  fg_gc       # a list of 5 graphics contexts - created during set_style() method
  bg_gc       # a list of 5 graphics contexts - created during set_style() method
  light_gc    # a list of 5 graphics contexts - created during set_style() method
  dark_gc     # a list of 5 graphics contexts - created during set_style() method
  mid_gc      # a list of 5 graphics contexts - created during set_style() method
  text_gc     # a list of 5 graphics contexts - created during set_style() method
  base_gc     # a list of 5 graphics contexts - created during set_style() method
  black_gc    # a list of 5 graphics contexts - created during set_style() method
  white_gc    # a list of 5 graphics contexts - created during set_style() method

  bg_pixmap   # a list of 5 GdkPixmaps

Each attribute can be accessed directly similar to style.black and style.fg_gc[gtk.STATE_NORMAL]. All attributes are read-only except for style.black, style.white, style.black_gc and style.white_gc.

An existing style can be copied for later modification by using the method:

  new_style = style.copy()

which copies the style attributes except for the graphics context lists and the light, dark and mid color lists.

The current style of a widget can be retrieved with:

  style = widget.get_style()

To change the style of a widget (e.g. to change the widget foreground color), the following widget methods should be used:

  widget.modify_fg(state, color)
  widget.modify_bg(state, color)
  widget.modify_text(state, color)
  widget.modify_base(state, color)
  widget.modify_font(font_desc)
  widget.set_style(style)

Setting the style will allocate the style colors and create the graphics contexts. Most widgets will automatically redraw themselves after the style is changed. If style is None then the widget style will revert to the default style.

Not all style changes will affect the widget. For example, changing the Label (see Section 9.1, “Labels”) widget background color will not change the label background color because the Label widget does not have its own gtk.gdk.Window. The background of the label is dependent on the background color of the label's parent. The use of an EventBox to hold a Label will allow the Label background color to be set. See Section 10.1, “The EventBox” for an example.