使用Ubuntu 12.04中的Python AppIndicator检测系统托盘中的滚轮信号

时间:2013-04-28 04:03:38

标签: python scroll appindicator

我正试图让这个python代码在鼠标悬停在托盘图标上并滚动鼠标滚轮时作出反应,我在网上找不到任何示例。这就是我到目前为止所做的,它对滚动滚轮没有反应......

#!/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()

如何检测滚轮运动?

1 个答案:

答案 0 :(得分:3)

这是连接到鼠标滚动事件的正确方法,代码确实有效,在两个12.04系统上进行了测试。可能有一个错误,因为其中一个测试运行的前几个测试也没有用,但之后很好。

如果你是从头开始我会建议使用pygobject(Gtk3)而不是pygtk(Gtk2),因为它不再开发。作为测试的一部分,我确实将您的脚本转换为pygobject并修复了显示菜单:

#!/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()