Sarthak Agarwal
Sarthak Agarwal

Reputation: 11

How do I know which of the menu items I have clicked in appindicator?

I have created a program in which I created several menu items and stored them in 'ci' last. Whenever I click one of the menu items it executes the 'checkStatus' function .I want to know which of the menu items I have clicked( ci[0] or ci[1] or something else) because my function will work as per the item I clicked.

#!/usr/bin/python

import requests
from bs4 import BeautifulSoup
import appindicator
import pynotify
import gtk

appindicator.CATEGORY_APPLICATION_STATUS)
a = appindicator.Indicator('tubecheck', 'indicator-messages', appindicator.CATEGORY_APPLICATION_STATUS)
a.set_label('Live cricket score')
a.set_status( appindicator.STATUS_ACTIVE )
m = gtk.Menu()
ci = []
url = 'http://static.cricinfo.com/rss/livescores.xml'
sc = requests.get(url)
soup = BeautifulSoup(sc.text,'lxml')
data = soup.select('item')
for i in range(len(data)):
    ci.append(gtk.MenuItem(str(i+1)+'. '+data[i].find('title').text))
    m.append(ci[i])
qi = gtk.MenuItem( str(i+2)+'. '+'Quit' )
m.append(qi)

a.set_menu(m)
for i in range(len(ci)):
    ci[i].show()
qi.show()

def checkStatus(item):
    pynotify.init('test')
    n = pynotify.Notification('Live Cricket score',data[i].text)
    n.show()


ci[i].connect('activate', checkStatus)

def quit(item):
    gtk.main_quit()

qi.connect('activate', quit)

gtk.main()

Upvotes: 0

Views: 123

Answers (1)

Bernmeister
Bernmeister

Reputation: 254

First, when you create each MenuItem, you can name that item:

menuItem = Gtk.MenuItem( "Visible name" )
menuItem.props.name = "internal unique name"

Then in the checkStatus function you can refer to the name using

item.props.name

and assuming you gave an unique name to each item, that should do it.

Upvotes: 0

Related Questions