Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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",
                                      "indicator-messages",
                         appindicator.IndicatorCategory.APPLICATION_STATUS)
    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):
        menu.append(MenuItem(label))
    menu.append(menu_quit)
    def quit_app(w):
        sys.exit(0)
    menu_quit.connect('activate', quit_app)
    menu_quit.show()
    ind.set_menu(menu)   
    Gtk.main()

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

share|improve this question
    
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. –  Ilya 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... –  Ilya Apr 29 '13 at 19:47

1 Answer 1

up vote 2 down vote accepted

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.

share|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. –  sparrow May 2 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. –  Ilya May 2 at 20:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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