K
K
KPEBETKA2015-02-17 22:09:00
Python
KPEBETKA, 2015-02-17 22:09:00

python+gtk. How to stop code when no menu is open?

I decided to make a simple application that will hang in the tray and, when you click on the icon, show a menu with the necessary information. But ran into a problem...

  • The add_item function is executed even when the menu is closed. To save energy, I would like the above function to be executed only if the menu is open
import pygtk, gtk, appindicator, glib

def add_item(ind):
  menu = gtk.Menu()
  item = gtk.MenuItem('%s' % time.time())
  menu.append(item)
  separator = gtk.SeparatorMenuItem()
  menu.append(separator)
  item = gtk.MenuItem("Settings")
  item.connect("activate", settings)
  menu.append(item)
  item = gtk.ImageMenuItem(gtk.STOCK_QUIT)
  item.connect("activate", quit)
  menu.append(item)
  menu.show_all()
  ind.set_menu(menu)
  return True

def settings(widget, data=None):
  print >> sys.stderr, 'settings'

def quit(widget, data=None):
  gtk.main_quit()


if __name__ == "__main__":
  ind = appindicator.Indicator ("totp-indicator", "indicator-totp", appindicator.CATEGORY_APPLICATION_STATUS)
  ind.set_status (appindicator.STATUS_ACTIVE)
  ind.set_attention_icon ("indicator-messages-new")
  ind.set_icon("gtk-dialog-authentication-panel")
  glib.timeout_add(1000,  add_item, ind)
  gtk.main()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2015-02-26
@vitosua

KPEBETKA and if divided into 2 streams? One is always executed, and the second, with a menu, is launched only when the icon is clicked.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question