Note that there are some explanatory texts on larger screens.

plurals
  1. POSet the hover background color of a Gtk3 MenuItem
    text
    copied!<p>I have a tray icon with a popup menu. I am trying to set the background color of the menu items in this popup. I am able to set the text color but not the background color of the menu item. </p> <p><img src="https://i.stack.imgur.com/sEe6i.png" alt="enter image description here"></p> <p>The background that appears is the default Ubuntu orange, and I can't override it. </p> <p>I've created a sample application that demonstrates this problem. Just copy-paste it into a .py file and it should run.</p> <pre><code>from gi.repository import Gtk, Gdk import sys class TrayIcon: def __init__(self): self.statusicon = Gtk.StatusIcon() self.statusicon.set_from_stock(Gtk.STOCK_MEDIA_PLAY) self.statusicon.connect("popup-menu", self.OnShowPopupMenu) window = Gtk.Window() def OnShowPopupMenu(self, icon, button, time): menu = Gtk.Menu() first = self.GetMenuItem("First") second = self.GetMenuItem("Second") menu.append(first) menu.append(second) menu.show_all() menu.popup(None, None, lambda w,x: self.statusicon.position_menu(menu, self.statusicon), self.statusicon, 3, time) def GetMenuItem(self, txt): menuItem = Gtk.MenuItem(txt) screen = Gdk.Screen.get_default() css_provider = Gtk.CssProvider() #css_provider.load_from_data("GtkWidget { color:white; background-color: green; } GtkWidget:hover,GtkWidget:selected { color:white; background-color:pink;}") css_provider.load_from_data("GtkMenuItem { color:#0f0; background-color: #f00; } GtkMenuItem:hover,GtkMenuItem:selected { color:#00f; background-color:#f00; font-weight:bold;}") context = Gtk.StyleContext() context.add_provider_for_screen(screen, css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION) menuItem.connect("button_press_event", self.exit) return menuItem def exit(self, a,b): sys.exit() TrayIcon() Gtk.main() </code></pre> <p>For GtkMenuItem the normal background and :hover background are being ignored. For GtkWidget the :hover background is being ignored. My aim is to prevent that Ubuntu orange from showing up without disabling the menu item. </p> <p><strong>Is there a way to set the background and hover/mouseover color of a GtkMenuItem? (without using 'import gtk')</strong></p> <p>I am using Ubuntu 12.04, default theme.</p> <p><strong>Edit1</strong>: To add a bit of clarity, this is what I am trying to do, but without 'import gtk'.</p> <pre><code> #Prevent background color when mouse hovers style = menuItem.get_style().copy() style.bg[gtk.STATE_SELECTED] = style.bg[gtk.STATE_NORMAL] menuItem.set_style(style) </code></pre> <p><strong>Edit2</strong>: I've also tried override_background_color() and modify_bg, and again, the orange still shows up on hover. Here are variants of what I have tried.</p> <pre><code> menuItem.override_background_color(Gtk.StateFlags.NORMAL,Gdk.RGBA(1.0,0.0,0.0,1)) menuItem.modify_bg(Gtk.StateFlags.NORMAL,Gdk.color_parse("red")) menuItem.override_background_color(Gtk.StateFlags.NORMAL, Gdk.RGBA(1.0, 1.0, 1.0, 1.0)) menuItem.override_background_color(Gtk.StateFlags.SELECTED, Gdk.RGBA(1.0, 1.0, 1.0, 1.0)) menuItem.override_background_color(Gtk.StateFlags.FOCUSED, Gdk.RGBA(1.0, 1.0, 1.0, 1.0)) </code></pre> <p><strong>Edit3</strong>: Answer has been provided, see <a href="https://stackoverflow.com/a/11491392/974369">this post</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload