PDA

View Full Version : Can someone give me a python gtk3 menu example?



MG&TL
February 10th, 2012, 07:02 PM
Hi-I've been trawling the web for a while to find an example of a menu in gtk3 for python. The only gtk3 tutorial I can find for python eschews menus entirely, suggesting I use XML and Gtk.Builder() instead.

Can someone provide a link, or, better still, an example? Thanks. (I'm looking for the file -> save variety, not the pane-switching variety.)

Hairy_Palms
May 3rd, 2012, 05:16 PM
this might be a bit late but i just happened to come across this from google, glade is really an easier way to do it for any complex menu structure http://python-gtk-3-tutorial.readthedocs.org/en/latest/menus.html

but you can also create it without glade as follows, this is a simple tray icon menu i made, but the menu part is perfectly valid for any other gtk3 purpose.


from gi.repository import Gtk

class IconoTray:
def __init__(self, iconname):
self.menu = Gtk.Menu()

APPIND_SUPPORT = 1
try: from gi.repository import AppIndicator3
except: APPIND_SUPPORT = 0

if APPIND_SUPPORT == 1:
self.ind = AppIndicator3.Indicator.new("rhythmbox", iconname, AppIndicator3.IndicatorCategory.APPLICATION_STATUS )
self.ind.set_status (AppIndicator3.IndicatorStatus.ACTIVE)
self.ind.set_menu(self.menu)
else:
self.myStatusIcon = Gtk.StatusIcon()
self.myStatusIcon.set_from_icon_name(iconname)
self.myStatusIcon.connect('popup-menu', self.right_click_event_statusicon)

def add_menu_item(self, command, title):
aMenuitem = Gtk.MenuItem()
aMenuitem.set_label(title)
aMenuitem.connect("activate", command)

self.menu.append(aMenuitem)
self.menu.show_all()

def add_seperator(self):
aMenuitem = Gtk.SeparatorMenuItem()
self.menu.append(aMenuitem)
self.menu.show_all()

def get_tray_menu(self):
return self.menu

def right_click_event_statusicon(self, icon, button, time):
self.get_tray_menu()

def pos(menu, aicon):
return (Gtk.StatusIcon.position_menu(menu, aicon))

self.menu.popup(None, None, pos, icon, button, time)

#test/debug stuff below here
def main():
app = IconoTray("rhythmbox")
app.add_menu_item(lambda x: Gtk.main_quit(), "Quit")
app.add_seperator()
app.add_menu_item(lambda x: Gtk.main_quit(), "Play")

if __name__ == '__main__':
Gtk.main()

main()