开发者

Python cpu temp in system tray Linux

I'm using tint2 for a panel and want to show the cpu 开发者_运维百科temp as a system tray icon since there aren't any plugins for tint2 that do that and I'd just like to know how to do this anyway whether there was one or not. The script I have so far is:

#! /usr/bin/python
import pygtk,os
pygtk.require("2.0")
import gtk
import egg.trayicon
t = egg.trayicon.TrayIcon("CPUTemp")
cpu_temp=os.popen('sensors | grep "temp1:" | cut -d+ -f2 | cut -c1-2').read()
t.add(gtk.Label(cpu_temp))
t.show_all()
gtk.main()

Basically, it works the first time around but I'd also like it to update every 5 seconds or so. Any help greatly appreciated.


you can define a timer via timeout_add_seconds and update your tray icon in the callback. See if an example below would work for you:

import gtk, gobject, os

class CPUTimer:
    def __init__(self, timeout):

        self.window = gtk.Window()
        vbox = gtk.VBox()
        self.window.add(vbox)
        self.label = gtk.Label('CPU')
        self.label.set_size_request(200, 40)
        vbox.pack_start(self.label)

        # register a  timer
        gobject.timeout_add_seconds(timeout, self.timer_callback)

        self.window.connect("destroy", lambda w: gtk.main_quit())
        self.window.connect("delete_event", lambda w, e: gtk.main_quit())

        self.window.show_all()
        self.timer_callback()

    def timer_callback(self):
        cpu_temp = os.popen('sensors | grep "temp1:" | cut -d+ -f2 | cut -c1-2').read()
        print 'update CPU: ' + cpu_temp
        self.label.set_text('CPU: ' + cpu_temp)
        return True

if __name__ == '__main__':
    timer = CPUTimer(1) # sets 1 second update interval
    gtk.main()

hope this helps, regards


Look at Python's "threading" module. Create a function which runs the command an update gtk.Label's text with new output(t.set_text(str)). And run this function on a thread.

http://docs.python.org/library/threading.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜