Results 1 to 2 of 2

Thread: Can someone give me a python gtk3 menu example?

  1. #1
    Join Date
    Jun 2011
    Location
    United Kingdom
    Beans
    Hidden!
    Distro
    Lubuntu Development Release

    Can someone give me a python gtk3 menu example?

    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.)

  2. #2
    Join Date
    Jul 2005
    Location
    London,England
    Beans
    1,022
    Distro
    Ubuntu Karmic Koala (testing)

    Re: Can someone give me a python gtk3 menu example?

    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.readthe...est/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.

    Code:
    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()
    Jeremy Clarkson - "A Dazzling Hero of Political Incorrectness"
    My LastFM profile
    want to Give each GTK program its own theme? well look Here

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •