Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8232913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:58:32+00:00 2026-06-07T17:58:32+00:00

I have a tray icon with a popup menu. I am trying to set

  • 0

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.

enter image description here

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T17:58:34+00:00Added an answer on June 7, 2026 at 5:58 pm

    After a lot of digging, the answer turned out to be somewhat obscure.

    • background-color does not work. I had to use background
    • I also had to use -unico-inner-stroke-width

    I ended up at a GTK3 CSS file for the Ubuntu theme itself to see what was happening, located here:

    /usr/share/themes/Ambiance/gtk-3.0/gtk-widgets.css
    

    I got the -unico-inner-stroke-width property from the CSS file above. I cannot determine why background-color in 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

                        GtkMenuItem 
                        {
                            border:@bg_color;
                            background:@bg_color;
                        }
    
                        GtkMenuItem:hover
                        {
                            background:@selected_bg_color;
                        }
    
                        GtkWidget
                        {
                            border: @bg_color;
                        }
    
                        #mymenu:hover
                        {
                            color:@fg_color;
                            background: @bg_color;
                            -unico-inner-stroke-width: 0;
                        }
    

    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 background property as per your needs.

    Result, a working MenuItem without a hover color

    enter image description here

    Here is the full Python 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)
            self.statusicon.set_tooltip_text("HELLO")
            window = Gtk.Window()
    
        def OnShowPopupMenu(self, icon, button, time):
    
            display = Gdk.Display.get_default()
            screen = display.get_default_screen()
    
            css_provider = Gtk.CssProvider()
    
            gtk3Css = """GtkMenuItem {
                            border:@bg_color;
                            background:@bg_color;
                        }
    
                        GtkMenuItem:hover
                        {
                            background:@selected_bg_color;
                        }
    
                        GtkWidget
                        {
                            border: @bg_color;
                        }
    
                        #mymenu:hover
                        {
                            color:@fg_color;
                            background: @bg_color;
                            -unico-inner-stroke-width: 0;
                        }"""
            css_provider.load_from_data(gtk3Css)
            context = Gtk.StyleContext()
            context.add_provider_for_screen(screen, css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
    
            menu = Gtk.Menu()
            #menu.set_name('mymenu')
            first = self.GetMenuItem("First")
            first.set_name('mymenu')
            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()
        menuItem.set_label(txt)
    
        menuItem.connect("button_press_event", self.exit)
    
        return menuItem
    
        def exit(self, a,b):
        sys.exit()
    
    
    TrayIcon()
    Gtk.main()
    

    If you wanted to set another background color, you could do this in the CSS above

                        GtkMenuItem:hover
                        {
                            background:purple;
                            -unico-inner-stroke-width: 0;
                        }
    

    (or do it in #mymenu:hover)

    enter image description here

    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,

    pkg-config --modversion gtk+-3.0
    

    But I do not have the expertise to determine the origin of this problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented tray icon menu for QT application. But the tray icon menu
I have a java application which has a tray icon in the top menu
I have tray icon and I've attached a menu to it so that when
I am trying to create an application that will have a tray icon only,
I have a Qt application with a system tray icon and a menu that
I have an application with tray icon. I am using notifyicon to do this
have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:
Current state: I have a tray icon, two context menus - one for about/options/exit/etc.
I have a Java app that has an icon in the system tray. How
I'm using NotifyIcon (System tray icon) in my WinForms application. I also have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.