pythonscrollappindicator

Detect scroll wheel signal in system tray using Python AppIndicator in Ubuntu 12.04


I'm trying to get this python code to react when the mouse hovers over the tray icon and scrolls the mouse wheel, I can't find any examples online. This is what I have so far, it doesn't react to scrolling the wheel...

#!/usr/bin/python

APPNAME = "My App"
ICON = "/usr/share/pixmaps/firefox.png"

import appindicator as AI
import gtk


def sayhello(item):
    print "menu item selected"


def scroll(aai, ind, steps):
    print "hello"  # doesn't print anything


def makemenu():
    ' Set up the menu '
    menu = gtk.Menu()
    check = gtk.MenuItem('Check')
    exit = gtk.MenuItem('Quit')
    check.connect('activate', sayhello)
    exit.connect('activate', gtk.main_quit)
    menu.append(check)
    menu.append(exit)
    return menu


def startapp():
    ai = AI.Indicator(APPNAME, ICON, AI.CATEGORY_APPLICATION_STATUS)
    ai.set_status(AI.STATUS_ACTIVE)
    ai.connect("scroll-event", scroll)
    ai.set_menu(makemenu())
    gtk.main()

startapp()

How can I detect scroll wheel movements?


Solution

  • That is the correct way to connect to the mouse scroll event and the code does work, tested on two 12.04 systems. There might be a bug however as the first few test runs on one of them did not work either but then was fine.

    If you are starting from scratch I would recommend using pygobject (Gtk3) instead of pygtk (Gtk2) as it is no longer developed. As part of testing I did convert your script to pygobject and fixed showing the menu:

    #!/usr/bin/env python
    
    APPNAME = "My App"
    ICON = "/usr/share/pixmaps/firefox.png"
    
    from gi.repository import AppIndicator3 as AI
    from gi.repository import Gtk
    
    def sayhello(item):
        print "menu item selected"
    
    def scroll(aai, ind, steps):
        print "hello" # doesn't print anything
    
    def makemenu():
        ' Set up the menu '
        menu = Gtk.Menu()
        check_item = Gtk.MenuItem('Check')
        exit_item = Gtk.MenuItem('Quit')
        check_item.connect('activate', sayhello)
        check_item.show()
        exit_item.connect('activate', Gtk.main_quit)
        exit_item.show()
        menu.append(check_item)
        menu.append(exit_item)
        menu.show()
        return menu
    
    def startapp():
        ai = AI.Indicator.new(APPNAME, ICON, AI.IndicatorCategory.HARDWARE)
        ai.set_status(AI.IndicatorStatus.ACTIVE)
        ai.set_menu(makemenu())
        ai.connect("scroll-event", scroll)
        Gtk.main()
    
    startapp()