user2325445
user2325445

Reputation: 75

Update a label in GTK, python

I am learning GTK at the moment. Feels like i am loosing my time with the documentation. And tutorials are pretty thin.

I am trying to make a simple app that can display my CPU's usage and temperature in near real time but i am stuck at updating the label. I am aware of set_label("text") but i don't understand how and where to use it. Needless to say, i am a complete noob.

Here is my sample code :

import subprocess
from gi.repository import Gtk
import sys

Class to get CPU data

class CpuData():

    # Get the CPU temperature
    @staticmethod
    def get_temp():
        # Get the output of "acpi -t"
        data = subprocess.check_output(["acpi", "-t"]).split()
        # Return the temperature
        temp = data[15].decode("utf-8")
        return temp


    # Get CPU usage percentage
    @staticmethod
    def get_usage():
        # Get the output of mpstat (% idle)
        data = subprocess.check_output(["mpstat"]).split()
        # Parses the output and calculates the % of use
        temp_usage = 100-float(data[-1])
        # Rounds usage to two decimal points
        rounded_usage = "{0:.2f}".format(temp_usage)
        return rounded_usage

Widow constructor

class MyWindow(Gtk.ApplicationWindow):

    # Construct the window and the contents of the GTK Window
    def __init__(self, app):
        # Construct the window
        Gtk.Window.__init__(self, title="Welcome to GNOME", application=app)
        # Set the default size of the window
        self.set_default_size(200, 100)

Label class

class MyLabel(Gtk.Label):
    def __init__(self):
        Gtk.Label.__init__(self)
        temp = CpuData.get_temp()
        # Set the label
        self.set_text(temp)

Application constructor

class MyApplication(Gtk.Application):

    def __init__(self):
        Gtk.Application.__init__(self)

    # Activate the window
    def do_activate(self):
        win = MyWindow(self)
        label = MyLabel()
        win.add(label)
        win.show_all()

    # Starts the application
    def do_startup(self):
        Gtk.Application.do_startup(self)

app = MyApplication()
exit_status = app.run(sys.argv)
sys.exit(exit_status)

Upvotes: 4

Views: 3453

Answers (1)

mame98
mame98

Reputation: 1341

You should call all your methods via a timeout:

GLib.timeout_add(ms, method, [arg])

or

GLib.timeout_add_seconds(s, method, [arg])

where ms are milliseconds and s are seconds (update interval) and method would be you getUsage and getTemp methods. You can pass the labels as arg.

Then you just have to call the set_text(txt) method on the label(s)

Note: You need to import GLib like this:

from gi.repository import GLib

EDIT #1

As @jku pointed out, the following methods are outdated and maybe just exist to provide backward compatability with legacy code (so you should not use them):


GObject.timeout_add(ms,method,  [arg])

or

GObject.timeout_add_seconds(s,method,  [arg])

EDIT #2

As your data methods get_temp and get_usage are more universal, you can use a little wraper function:

def updateLabels(labels):
    cpuLabel  = labels[0]
    tempLabel = labels[1]
    cpuLabel.set_text(CpuData.get_usage())
    tempLabel.set_text(CpuData.get_usage())
    return False

Then simply do:

GLib.timeout_add_seconds(1, updateLabels, [cpuLabel, tempLabel]) # Will update both labels once a second

EDIT #3

As I said, here is the example code:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import GLib
import time


class TimeLabel(Gtk.Label):
    def __init__(self):
        Gtk.Label.__init__(self, "")
        GLib.timeout_add_seconds(1, self.updateTime)
        self.updateTime()


    def updateTime(self):
        timeStr = self.getTime()
        self.set_text(timeStr)
        return GLib.SOURCE_CONTINUE

    def getTime(self):
        return time.strftime("%c")


window = Gtk.Window()
window.set_border_width(15)
window.connect("destroy", Gtk.main_quit)

window.add(TimeLabel())
window.show_all()
Gtk.main()

Upvotes: 6

Related Questions