views:

42

answers:

1

I am trying to get an image to appear next to a menu item but it isn't working.

In order to make this as simple as possible, I have created a very simple example below that highlights the problem:

import pygtk
pygtk.require('2.0')
import gtk

class MenuExample:

    def __init__(self):

        window = gtk.Window()
        window.set_size_request(200, 100)
        window.connect("delete_event", lambda w,e: gtk.main_quit())

        menu = gtk.Menu()

        menu_item = gtk.ImageMenuItem("Refresh")

        img = gtk.image_new_from_stock(gtk.STOCK_REFRESH, gtk.ICON_SIZE_MENU)
        img.show()
        menu_item.set_image(img)

        menu.append(menu_item)

        menu_item.show()

        root_menu = gtk.MenuItem("File")

        root_menu.show()
        root_menu.set_submenu(menu)

        vbox = gtk.VBox(False, 0)
        window.add(vbox)
        vbox.show()

        menu_bar = gtk.MenuBar()
        vbox.pack_start(menu_bar, False, False, 2)
        menu_bar.show()

        menu_bar.append(root_menu)

        window.show()

def main():
    gtk.main()
    return 0

if __name__ == "__main__":
    MenuExample()
    main()

When I run the application, it shows the menu item, but it does not show the image next to it.

OS: Ubuntu 10.04 64-bit
Python version: 2.6.5

+1  A: 

Hmmm... it turns out the answer was that my desktop theme had disabled icons for menus. (Who knows why.)

After enabling the option, the icons now show up.

George Edison
You should accept your answer -- after all, it did answer the question!
katrielalex
@kat: I can't accept my own answer until 48 hours after I posted it.
George Edison
@George: sorry, I forgot about that! =)
katrielalex
This is the default in Gnome these days.
Oli