There is a similar question at StackOverflow, but it didn't help me: Changing items while running. I tried asking at StackOverflow, but people there where not too active: How to change GTK menu in Unity Indicator Applet?.

The problem is, if I define the changing function outside of Gtk.main(), it would be never executed, and inside it would be executed just once. If I make an infinite loop inside, Gtk.main() would be never executed. This is my code (modified standard example).

#!/usr/bin/env python

import sys

from gi.repository import Gtk, Gdk
from gi.repository import AppIndicator3 as appindicator

if __name__ == "__main__":
    ind = appindicator.Indicator.new ("example-simple-client",
    ind.set_status (appindicator.IndicatorStatus.ACTIVE)
    ind.set_attention_icon ("indicator-messages-new")

    menu = Gtk.Menu()
    menu_quit = Gtk.MenuItem('Quit')
    def add_item(menu, label):
    def quit_app(w):
    menu_quit.connect('activate', quit_app)

Where do I put adding new menu items (or changing existing) in the code? I am a real newbie in GUI programming...

  • How and when do you want to call add_item()? You should give a bit more information. – Timo Apr 29 '13 at 9:49
  • I want to call it, when user gets something in clipboard. – Phlya Apr 29 '13 at 11:23
  • I think this is quite a broad question, actually, as the same applies to any GTK menus, not only AppIndicator, and even not only menus, but, actually, any GTK widgets. I really need to get a hold on that, I think... – Phlya Apr 29 '13 at 19:47

One has to use

GLib.timeout_add(10, handler_timeout)

inside the indicator code, where 10 - is 10 milliseconds, time between refreshing, and handler_timeout - function to be called at each refresh. Inside this function one can do all the needed stuff, like checking for clipboard changes and editing the menu.

| improve this answer | |
  • How did you solve this problem? I have similar issues and using GLib's timeout_add method gives me Segmentation fault error. – moshfiqur May 2 '14 at 16:45
  • I can't really help, because I didn't get a Segmentation fault when used it... It just worked like a charm really. – Phlya May 2 '14 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.