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.

The background that appears is the default Ubuntu orange, and I can’t override it.
I’ve created a sample application that demonstrates this problem. Just copy-paste it into a .py file and it should run.
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()
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.
Is there a way to set the background and hover/mouseover color of a GtkMenuItem? (without using ‘import gtk’)
I am using Ubuntu 12.04, default theme.
Edit1: To add a bit of clarity, this is what I am trying to do, but without ‘import gtk’.
#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)
Edit2: 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.
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))
Edit3: Answer has been provided, see this post.
After a lot of digging, the answer turned out to be somewhat obscure.
background-colordoes not work. I had to usebackground-unico-inner-stroke-widthI ended up at a GTK3 CSS file for the Ubuntu theme itself to see what was happening, located here:
I got the
-unico-inner-stroke-widthproperty from the CSS file above. I cannot determine whybackground-colorin my script is being ignored, but it is, at least on Ubuntu 12.04.I was also forced to ‘re-set’ the background and border colors of the elements I was changing as they would otherwise appear strange. This is the minimum CSS I had to use
In this example, I am setting the hover color of a single GtkMenuItem to be the same as the background color, but if you want to set the color to something else, you’d have to change the
backgroundproperty as per your needs.Result, a working MenuItem without a hover color
Here is the full Python code:
If you wanted to set another background color, you could do this in the CSS above
(or do it in #mymenu:hover)
In conclusion, I think this may have been a problem limited to Ubuntu 12.04 or GTK 3.4.2 which I determined by running,
But I do not have the expertise to determine the origin of this problem.