2

I'm starting to write a small panel applet for Gnome and I'd like the user to be able to left-click on the status icon to see some options and information e.g. similar to sound icon in Gnome 3, where you can set volume via left-click while set preferences via right-click. Right-click code is this:

statusicon.connect("popup-menu", right_button_click) 

where right_button_click is the name of the function that gets called on right-click event. The important part is "popup-menu". What would be alternative for setting left-click event?

3 Answers 3

5

This is the tiny example showing how it works.

#!/usr/bin/python3 import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk class TrayIcon(Gtk.StatusIcon): def __init__(self): Gtk.StatusIcon.__init__(self) self.set_from_icon_name('help-about') self.set_has_tooltip(True) self.set_visible(True) self.connect("popup_menu", self.on_secondary_click) def on_secondary_click(self, widget, button, time): menu = Gtk.Menu() menu_item1 = Gtk.MenuItem("First Entry") menu.append(menu_item1) menu_item2 = Gtk.MenuItem("Quit") menu.append(menu_item2) menu_item2.connect("activate", Gtk.main_quit) menu.show_all() menu.popup(None, None, None, self, 3, time) if __name__ == '__main__': tray = TrayIcon() Gtk.main() 
Sign up to request clarification or add additional context in comments.

Comments

1

First thing is to look into the gnome code for the volume control, and that's is this Second, you should look into the API documentation for GtkStatusIcon, and that one is here That should be enough.

2 Comments

I need to do this in python with PyGObject (successor of PyGtk).
@Rainbow It is almost the same thing, since PyGObject are bindings for Gtk+ c library.
0

This is a late response, but i'm just posting this in case someone else ever looks for left click control over gtkstatusicon.

The direct alternative is

statusicon.connect("activate", left_button_click) 

This is a sample for trayicon popup menu working with left click instead of the (common) right click.

#!/usr/bin/env python import pygtk pygtk.require('2.0') import gtk class TrayIcon(gtk.StatusIcon): def __init__(self): gtk.StatusIcon.__init__(self) self.set_from_icon_name('help-about') self.set_has_tooltip(True) self.set_visible(True) self.connect("activate", self.on_click) def greetme(self,data=None): msg=gtk.MessageDialog(None, gtk.DIALOG_MODAL,gtk.MESSAGE_INFO, gtk.BUTTONS_OK, "Greetings") msg.run() msg.destroy() def on_click(self,data): event=gtk.get_current_event() btn=event.button #this gets the button value of gtk event. time=gtk.get_current_event_time() # required by menu popup. menu = gtk.Menu() menu_item1 = gtk.MenuItem("First Entry") menu.append(menu_item1) menu_item1.connect("activate", self.greetme) menu_item2 = gtk.MenuItem("Quit") menu.append(menu_item2) menu_item2.connect("activate", gtk.main_quit) menu.show_all() menu.popup(None, None, None, btn, time) #button can be hardcoded (i.e 1) but time must be correct. if __name__ == '__main__': tray = TrayIcon() gtk.main() 

Also , there is this alternative :

statusicon.connect("button-press-event", button_click) 

Bellow sample code raise the same popup menu in gtktrayicon in BOTH right and left click.

#!/usr/bin/env python import pygtk pygtk.require('2.0') import gtk class TrayIcon(gtk.StatusIcon): def __init__(self): gtk.StatusIcon.__init__(self) self.set_from_icon_name('help-about') self.set_has_tooltip(True) self.set_visible(True) self.connect("button-press-event", self.on_click) def greetme(self,data=None): msg=gtk.MessageDialog(None, gtk.DIALOG_MODAL,gtk.MESSAGE_INFO, gtk.BUTTONS_OK, "Greetings") msg.run() msg.destroy() def on_click(self,data,event): #event in this case is sent by the status icon connect. btn=event.button #By controlling this event.button value (1-2-3 for left-middle-right click) you can call other functions. time=gtk.get_current_event_time() # required by the popup. menu = gtk.Menu() menu_item1 = gtk.MenuItem("First Entry") menu.append(menu_item1) menu_item1.connect("activate", self.greetme) menu_item2 = gtk.MenuItem("Quit") menu.append(menu_item2) menu_item2.connect("activate", gtk.main_quit) menu.show_all() menu.popup(None, None, None, btn, time) if __name__ == '__main__': tray = TrayIcon() gtk.main() 

Hope above code helps. George V.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.