tags:

views:

55

answers:

2

I creating applet in gnome panel. All code is good. But info in panel is static. But need refresh this info in time. 1 secon or 5 second...

Here is part of python code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import gobject
import gtk
import pygtk
import gnomeapplet
import time
import urllib2
pygtk.require('2.0')

def applet_factory(applet, iid):   
   label = gtk.Label("Simple text")
   applet.add(label)
   applet.show_all()

print('Factory started')

if __name__ == '__main__':   # testing for execution
   print('Starting factory')
   gnomeapplet.bonobo_factory('OAFIID:SampleApplet_Factory', 
                              gnomeapplet.Applet.__gtype__, 
                              'Sample Applet', '0.1', 
                              applet_factory)

I need refresh "simple text" label in time interval. How did that?

A: 

Most GUI libraries (wxPython, GTK, Qt, ...) let you define timers, so that your function that sets the label text is called every 5 seconds. See accepted answer in this SO question: http://stackoverflow.com/questions/1052574/run-a-function-every-x-minutes-python

tobiw
Not working for my script, can you write full code wit refresh code part?
track
Works for me but I get a deprecated warning for gtk.timeout_add
tobiw
Yes, and i get this error
track
A: 

What about...:

def applet_factory(applet, iid):   
   label = gtk.Label("Simple text")
   applet.add(label)
   applet.show_all()
   return label

thelabel = applet_factory(applet, iid)

def redrawlabel(*args):
    thelabel.queue_draw()
    # process all events
    while gtk.events_pending():
        gtk.main_iteration(False)
    return True

# call redrawlabel every 5 minutes
gtk.timeout_add(5*60*1000, redrawlabel)

If you also need to set the text to something different, thelabel.set_text('something else') will do that.

Alex Martelli
File "applet/src/applet.py", line 43 return labelSyntaxError: 'return' outside function
track
The two `return` statements I show in the above code are inside functions, so I don't know what error you made in transcribing my code (since of course I don't know which line is 43 nor what the other 42+ ones around it are -- how could I possibly know since you decide to hide that?).
Alex Martelli
I editen Question in full script
track
Have any ideas?
track
Alex Martelli
43 line is not required, this all script generated "Simple text". U can solve proble with this part?
track
If there is no line 43 then I assure you that you will **not** get a syntax error there -- so remove everything beyond what you've shown in your Q and what I've shown in my A and let us know.
Alex Martelli