0

当鼠标悬停在托盘图标上并滚动鼠标滚轮时,我试图让这个 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()

如何检测滚轮运动?

4

1 回答 1

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()
于 2013-05-03T15:22:42.537 回答