PyGTK: TemperatureWatch [update]

Posted on 17 December 2009 - 14:52 in Programming - Comments (1)

Display live CPU Temperature and Fan speed in a GTK window.
Working on Ubuntu, ArchLinux and Thinkpad T60, probably others too.

Version 1.1 released:
- Includes Fan speed

temperaturewatch

Code below or here

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Display live CPU Temperature in a GTK window.
# Working on Ubuntu, ArchLinux and Thinkpad T60, probably others too.
#
# Version: 1.1
# Requires: python >=2.5, python-gtk2, python-gobject
# /proc fs with thinkpad_acpi module enabled
#
# Copyright (C) 2009 wag@wag-net.ch
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
 
 
import gtk, gobject, pango
gobject.threads_init()
import subprocess as sp, re
 
 
class TemperatureWatch():
    def __init__(self):
        # Set path to cpu thermal_zone proc file, or leave blank
        self.p_CPU0 = "/proc/acpi/thermal_zone/THM0/temperature"
        self.p_CPU1 = "/proc/acpi/thermal_zone/THM1/temperature"
        self.p_FAN = "/proc/acpi/ibm/fan"
 
 
        gobject.timeout_add(500, self.update)
        self.win()
 
 
    def update(self):
        """Update labels every half second"""
        # Execute system call, regex for numbers, set label
        if self.p_CPU0:
            CPU0 = sp.Popen(["cat", self.p_CPU0], stdout = sp.PIPE).communicate()[0]
            rCPU0 = re.search("(\d+)", CPU0)
            self.l_CPU0.set_text("%s °C" % rCPU0.group(0))
 
        if self.p_CPU1:
            CPU1 = sp.Popen(["cat", self.p_CPU1], stdout = sp.PIPE).communicate()[0]
            rCPU1 = re.search("(\d+)", CPU1)
            self.l_CPU1.set_text("%s °C" % rCPU1.group(0))
 
        if self.p_FAN:
            FAN = sp.Popen(["cat", self.p_FAN], stdout = sp.PIPE).communicate()[0]
            rFAN = re.search("(\d+)", FAN)
            self.l_FAN.set_text("%s RPM" % rFAN.group(0))
 
        return True
 
 
    def win(self):
        """Create window"""
        win = gtk.Window(gtk.WINDOW_TOPLEVEL)
        win.connect("destroy", lambda q: gtk.main_quit())
        win.set_title("TemperatureWatch")
        win.set_border_width(20)
 
 
        # Box, AspectFrame, Table
        vb = gtk.VBox(False, 0)
        win.add(vb)
 
        af = gtk.AspectFrame()
        af.set_border_width(2)
        vb.pack_start(af)
 
        table = gtk.Table(2, 3, False)
        win.set_geometry_hints(table, min_width=150, min_height=120)
        table.set_row_spacings(5)
        table.set_col_spacings(5)
        af.add(table)
 
 
        # Labels
        l__CPU0 = gtk.Label("CPU0 : ")
        l__CPU0.set_padding(5, 5)
        l__CPU0.modify_font(pango.FontDescription("16"))
        table.attach(l__CPU0, 0, 1, 0, 1)
 
        l__CPU1 = gtk.Label("CPU1 : ")
        l__CPU1.set_padding(5, 5)
        l__CPU1.modify_font(pango.FontDescription("16"))
        table.attach(l__CPU1, 0, 1, 1, 2)
 
        l__FAN = gtk.Label("FAN : ")
        l__FAN.set_padding(5, 5)
        l__FAN.modify_font(pango.FontDescription("16"))
        table.attach(l__FAN, 0, 1, 2, 3)
 
        self.l_CPU0 = gtk.Label("00 °C")
        self.l_CPU0.set_padding(5, 5)
        self.l_CPU0.modify_font(pango.FontDescription("16"))
        table.attach(self.l_CPU0, 1, 2, 0, 1)
 
        self.l_CPU1 = gtk.Label("00 °C")
        self.l_CPU1.set_padding(5, 5)
        self.l_CPU1.modify_font(pango.FontDescription("16"))
        table.attach(self.l_CPU1, 1, 2, 1, 2)
 
        self.l_FAN = gtk.Label("0000 RPM")
        self.l_FAN.set_padding(5, 5)
        self.l_FAN.modify_font(pango.FontDescription("16"))
        table.attach(self.l_FAN, 1, 2, 2, 3)
 
 
        # Button
        b_quit = gtk.Button(" Quit ")
        b_quit.set_border_width(2)
        b_quit.child.modify_font(pango.FontDescription("11"))
        vb.pack_start(b_quit)
        b_quit.connect("clicked", lambda q: gtk.main_quit())
 
 
        win.show_all()
        gtk.main()
 
 
 
TW = TemperatureWatch()

1 Comment »

  1. #1 — Comment by tim — 9 May 2010 - 14:45

    Works too on x200s…

    little detail: add the “utf-8 coding” on top of the script… Without this it doesn’t work on mine laptop…

    the line:
    # -*- coding: utf-8 -*-

    but in general nice done!:-)
    tim
    -
    isch ja dinne :>


RSS Comments - TrackBack URL

Leave a comment

This work is licensed under a Creative Commons Attribution 2.5 Switzerland License
Powered by WordPress