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)