[pygtk] Trouble keeping a menu open on right-click of a menu-item

Nate e70423 at gmail.com
Mon Sep 11 05:27:03 WST 2006


Hi, I'm a little new to pygtk; I am only about a month or so in. I'm 
trying to create a favorites, or bookmarks menu for an application. What 
I'm trying to do is have it so when the user right clicks the item, a 
menu pops up over the item with "rename" and "delete."

I'm having problems keeping the favorites menu open on a right-click. I 
also can not make the pop-up menu appear. I am not sure whether the 
first problem causes the second one or not.

Below is some source which shows my attempts. I've already figured out 
how to deal with right and left clicks.

The below code is standalone. It works by itself, so you can just try it 
out yourself, and it'll work (mostly). What it's lacking is that it 
doesn't make the menu pop up and it closes the favorites menu after 
right-clicking a button.

As you can see below, I've created the popup menu and tried to implement 
it. But  I have no idea where to start as to not closing the favorites 
menu after one of its items is right clicked.

Can anyone help me with this? How do I fix this so that, when an item on 
the menu is right-clicked, the menu stays open, and the popup 
"rename/delete" menu appears?

---CODE BELOW---

import gtk

#make window
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
box = gtk.VBox()
window.add(box)

#create menubar, menubutton
menu_bar = gtk.MenuBar()
bookmarkbutton = gtk.MenuItem("bookmarks")
menu_bar.append(bookmarkbutton)

#the variable used to hold the kind of mouse click
mousebutton = 0

def get_button(menu, event):
    '''When a click is detected on the favorites menu, see which kind it 
is'''
    global mousebutton
    mousebutton = event.button

def bookmark_select(bookmark, menu, mousebutton):
    '''when the bookmark is actually activated, depending on the kind of 
click used, take different actions'''
    def printer(text='a'):
        print text
        
    if mousebutton == 1 or mousebutton == 2:
        '''if the user clicked the item with their left or middle mouse 
button'''
        print "Left clicked " + bookmark.label
    elif mousebutton == 3:
        '''if the user clicked the item with their right mouse button'''
        print "Right clicked " + bookmark.label + ", favorites menu 
should stay open, and a popup menu should appear over it with 'rename' 
and 'delete'."
        popup_menu = gtk.Menu()
        rename_bookmark = gtk.MenuItem('rename ' + bookmark.label)
        rename_bookmark.connect_object('activate', printer, 'You clicked 
"rename %s"' % (bookmark.label))
        delete_bookmark = gtk.MenuItem('delete ' + bookmark.label)
        delete_bookmark.connect_object('activate', printer, 'You clicked 
"delete %s"' % (bookmark.label))
        popup_menu.append(rename_bookmark)
        popup_menu.append(delete_bookmark)
        popup_menu.popup(menu, bookmark, None, mousebutton, 2)
        popup_menu.show()
        rename_bookmark.show()
        delete_bookmark.show()

#Create the menu, connect clicks on it to the get_button function. Add 
it to the bookmark button.
menu = gtk.Menu()
menu.add_events(gtk.gdk.BUTTON_PRESS_MASK)
menu.connect('button_press_event', get_button)
bookmarkbutton.set_submenu(menu)

#Make items on the menu. Connect activations of them to a function which 
performs different tasks depending on right or left click.
menu_items = []
for i in range(0, 3):
    menu_items.insert(i, gtk.MenuItem("bookmark " + str(i)))
    menu_items[i].label = menu_items[i].get_child().get_label()
    menu_items[i].connect('activate', lambda x: bookmark_select(x, menu, 
mousebutton))
    menu.append(menu_items[i])

#show everything
window.show()
box.pack_start(menu_bar, expand=False, fill=False)
box.show()
menu_bar.show()
bookmarkbutton.show()
menu.show()
for i in menu_items:
    i.show()

gtk.main()


---CODE ABOVE---

Thanks for any help.
-Nate


More information about the pygtk mailing list