0

For a pop-up menu made in Gtk, I would like to have the first menu item as a header. Preferably its background should be white. Since---according to the documentation---one cannot change a gtk.Label's background colour, but rather must change its container's background, it seemed to me the gtk.MenuItem itself should be modified.

However, I have tried the following in vain:

menu_item.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse('#FFFFFF'))

This would work for a container as gtk.EventBox, but for gtk.MenuItem it does not. What's not working here above and what can I do to have this gtk.MenuItem background white?

PS: i'd rather not use any .rc file for this.

neydroydrec
  • 6,973
  • 9
  • 57
  • 89
  • looks like you can't. I tried that in C with no luck :( I didn't look into the source code but I guess MenuItem doesn't have the "NORMAL" state? If you change the state argument to `STATE_SELECTED`, it does changes the background color on mouse over. – qingbo Aug 26 '11 at 06:50
  • This is so why I don't like Gtk (it feels like things are made so one can't style one's GUI). Thanks + perhaps you should post this as an answer. – neydroydrec Aug 26 '11 at 08:18
  • @Benjamin, things _are_ made so you can't style your GUI. The design philosophy of GTK is that theming should be left up to the user, and applications should present a consistent look. Also, one of the big selling points of GTK is accessibility. If you make your fonts small and use colors indiscriminately, you are locking out users with poor vision or color blindness. If users want colors, they will install a colorful theme, so goes the reasoning. – ptomato Sep 11 '11 at 16:54

2 Answers2

1

Here is a sample that puts the "exit" menu in white when mouse hovvers over it. Hope it can help you !

#!/usr/bin/python

import gtk

class PyApp(gtk.Window):

    def __init__(self):
        super(PyApp, self).__init__()

        self.set_title("Simple menu")
        self.set_size_request(250, 200)
        self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color(6400, 6400, 6440))
        self.set_position(gtk.WIN_POS_CENTER)

        mb = gtk.MenuBar()

        filemenu = gtk.Menu()
        filem = gtk.MenuItem("File")
        filem.set_submenu(filemenu)

        exit = gtk.MenuItem("Exit")
        style = exit.get_style().copy ()
        style.bg[gtk.STATE_NORMAL] = exit.get_colormap().alloc_color (0xffff, 0x0000, 0x0000)
        exit.set_style (style)

        exit.connect("activate", gtk.main_quit)
        filemenu.append(exit)

        mb.append(filem)

        vbox = gtk.VBox(False, 2)
        vbox.pack_start(mb, False, False, 0)

        self.add(vbox)

        self.connect("destroy", gtk.main_quit)
        self.show_all()

PyApp()
gtk.main()

To do this, I play with the "style".

Louis
  • 2,854
  • 2
  • 19
  • 24
  • Thanks :) When I try this however, the foreground colour (text) changes and not the background. Is that what it does with you too? From the `self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color(6400, 6400, 6440))` and `style.bg[gtk.STATE_NORMAL] = exit.get_colormap().alloc_color (0xffff, 0x0000, 0x0000)` statements I take it the background should change, right? – neydroydrec Sep 05 '11 at 14:53
  • Indeed, it is the background that changes for me, turning white (and making the white text on it impossible to read!). Maybe have a look at the "theme" used. And it's probably worth to try to change this color with the "theme" so that some poor laptop user may still be able to change the settings to use it. – Louis Sep 05 '11 at 15:19
  • Indeed it seems the easiest way to go about changing styles in Gtk is with the resource files. It's possible the current theme on my machine overrides local changes. And that's also why I so prefer Qt to Gtk :) – neydroydrec Sep 05 '11 at 16:19
0

After messing around with it, I found this will work to change the text on the main menu:

menu_item.child.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse('#FFFFFF'))

But if you want the whole background of the menu white, you need to change the parent menu, not the MenuItem, like this:

menu = gtk.Menu()
menu_item = gtk.MenuItem("File")
menu_item.set_sumenu(menu)
menu.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color(65355,65355,65355))
Sean
  • 1